This article was (slightly modified) first published on my personal blog.
When working with Git, a lot of us don’t type Git commands in the terminal anymore, but we are using a Git client that has a user interface. This is cool and all but knowing Git commands and working directly in the terminal has it’s perks:
- it’s faster than clicking; thus you are more productive
- you aren’t limited by the options that the UI is offering you. Knowing Git commands makes you independent from the Git client options and you are also independent from potentially changes in the UI
- knowing Git on IntelliJ, SourceTree, or GitKraken usually means you know the workflow on these platforms, but by learning Git commands you can apply this knowledge to other platforms, too.
- this is probably just personal taste, but I think using Git commands in the terminal just looks cool. 😎
In this article I’ll go beyond the basics to advanced git commands that, if you are using them, makes it easy for others to believe you are a Git expert. Also, they will help you to increase your productivity. So, even if you are quite comfortable using Git, you still may find this article helpful.
Below are less known but handy Git commands to boost your productivity and maybe even impress your colleagues.
git checkout –
If you are switching back and forth different branches multiple times, you would have to write
git checkout branch-1 and
git checkout branch-2 over and over again. Since developers hate repetition you can use
git checkout - .
So, when switching to the previous branch you just need to write
git checkout - . Switching back and forth different branches means, you only have to write
git checkout - once every time you want to switch to the other branch. Quite handy, don’t you think?
git add -p
Most likely you have used
git add . before. That command means that you add every file you changed to the commit at once. But what if you want to be more selective of what you want to commit? Or what if you want to go one by one of every changes you have made before committing?
For this you have
git add -p . The
-p stands for “patch”. Using
git add -p you don’t add all your changes at once but in small “patches” that you can decide whether you want to add to your commit or not. Git will ask you for every patch whether you want to add or not. You decide by typing
y for “yes” or
n for “no”.
Searching for the last working commit can be tedious if you have to checkout every single commit.
git bisect makes it easy for you to find the last working commit by basically using binary search.
In order to do that, you have to activate
git bisect by typing
git bisect start . After that you type in
git bisect good for a commit that you know is working correctly. Once you have done that you type in
git bisect bad for a commit that you know is not working anymore (usually this is your last commit).
When you write
git bisect bad Git will checkout an old commit that is in the middle of your history between the first good commit and the last bad commit until it finds the first bad commit.
git commit –amend
Sometimes you want to change a commit message that you already pushed or you forgot to add a small commit that is so related to your last commit that you don’t want to create a commit of it’s own. In this case you can use
git commit --amend .
git commit --amend you can either change the last commit message or you can add an additional change to the last commit. If you want to push your changes, though, you have to use
git push -f in either case.
git rebase -i HEAD~n
git commit --amend allows you to change the last commit message. What if you want to change a commit message that was made way before that?
git rebase -i HEAD~n to your rescue. By typing
git rebase -i HEAD~n you can go back to the n th commit and change it by typing edit to the commit you want to modify.
Once you are done and want to save your changes, you have to use
git push -f in order to overwrite them.
Have you used them before? What do you think about these Git commands? Do you know more less known but handy Git commands? Or do you have any questions? Comment below. I’m super interested what other Git commands you also find useful.