Git is probably the most important part of a developer's life. Along with Stack overflow and Google of course but in terms of storage of data, collaborative development and many other features that git provides it's an essential skill to learn.
Although there are many used git commands there are a few that most people don't use but are equally important and may make your life easier by knowing them.
So today I'm writing about six least used git commands that may make your life easier and learning more fun.
1- git add -p:
We use git add to staging commits. But did you know you can stage a section of code instead of the entire code again and again? Well that's exactly what git add -p does.
When the code is run there we are asked for a confirmation- "Stage this hunk [y, n, g, a, d, e, ?]?"
What do these letters mean?
Here you go:
y: stage this hunk
n: do not stage this hunk
g: select a different hunk to go to
a: stage this hunk and all the other hunks in this file
d: do not stage this hunk or any of the other hunks in this file
e: edit the current hunk manually
?: print help
Hunk is basically a chunk of code.
git add -p <temp.txt>
2- git commit -amend:
Have you clicked 'Enter' too fast and realised that you made a typo in the commit message?
I do that a lot and when I found out about this command I was shocked.
Let's say you made a typo in the commit something like
git commit -m 'fix tyepo'
Now you can fix that using:
git commit -amend -m 'fix typo'
3- git stash:
It's one of the interesting commands that I came across. More than often we commit our changes then think that it might have looked better this way and then you the stuff all over again. Well you don't have to worry about that now. All you have to do is git stash and go your way and do all the testing that you want to do. And when you're ready to commit you can just run git stash pop and your code will be back like nothing ever happened.
git stash
4- git cherry-pick:
This command allows us to bring a commit of one branch to another.
Let's say you have committed a branch b_test and you want the commit of that branch into b_test2 now you can do that using git cherry-pick b_test.
Some commands used with cherry-pick:
git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] [--ff]
[-S[<keyid>]] <commit>β¦β
git cherry-pick (--continue | --skip | --abort | --quit)
5- git rm:
The command helps us remove files or directories. There are two ways you can take: force and cache. The forced path deletes the files/directory altogether. The cached path removes the files from the working directory. You have to choose one or ther other while using the command.
git rm filename.txt
6- git checkout:
Git checkout is almost similar to git cherry-pick. The difference is that we can go even deeper with this command. There you were pulling the whole commit and sometimes these commits are too big, so instead of that git checkout helps pull only a particular file that is needed.
git checkout <branch-name>
These are some commands that I've come across. If you have come across similar commands then do let me know in the comments and share it if you got some new knowledge about git.
Top comments (2)
Great post! Very useful commands! π
Thanks Arthur!