Master these 5 Git commands like a pro: No more Googling!
As Software Engineers, Git is an essential tool that we use every day for version control. Most of the people reading this are for sure familiar with the basic Git commands such as git add
, git commit
, git push
, and git pull
. These are the most friendly ones and no-brainer — we run these with muscle memory.
And then there are the git commands that we look up only in the times of absolute catastrophe — when we have to Google a situation and hope that the most upvoted answer on StackOverflow works.
But what if I tell you that there’s a grey area — which consists of some git commands which are powerful yet usable every day. And with experience I can tell, these are the ones that will set you apart. Using lesser-known git commands with confidence is indeed a flex!
“A genius is the man who can do the average thing when everyone else around him is losing his mind.“ — Napoleon
“git checkout .” — The CTRL+Z
Before you judge the heading, just know that checkout
is the Swiss Army Knife in git. It is very powerful and can do several tasks with ease. Out of which, checkout .
tops my favourite list.
git checkout .
This command resets all the files in the current working directory to the state they were in when the last commit was made. This is useful when you have made changes to multiple files and want to revert them all at once. Personally, I use it when I have to make temporary changes to code and run the output — good for experiments!
Note that this command will permanently undo any changes that haven’t been committed or staged yet.
💡 Bonus Tip: In case you want to undo staged changes, use it preceded by a git reset .
command.
“git revert” — The bigger CTRL+Z
In the above section we saw how we can make the use of git reset .
and git checkout .
when we have to undo the uncommitted changes. But what if we did commit? git revert
will rescue you.
git revert [commit]
You have to run this command along with the commit hash that you want to revert. Consider the example in the screenshot below.
Note that reverting a commit will not remove the commit from your tree. Instead, it will create a “revert commit” that would be exactly opposite to what you did in the specified commit.
💡 Bonus Tip: In case you want to do something before creating the reverted commit, use git revert -no-commit [commit]
or git revert -n [commit]
to make changes before making the revert commit.
To get the commit hash, use git log
. We will discuss this in a later part of this blog.
“git cherry-pick” — The saviour
Created a branch out of master instead of development? Made several commits to it? Happens to the best of us..
git cherry-pick [commit]
With cherry-pick
you can pick a particular commit from any branch and append that to your working tree without the need of copy-pasting anything. You have to specify the hash of the commit to pick, just like we did in revert
.
💡 Bonus Tip: You can also pick a range of commits at once using git cherry-pick [commit a]^..[commit b]
where commit a
is the older commit.
“git push” — Replacing branches
I hate it when I make a typo in a branch name, and who doesn’t? It gets more frustrating (and sometimes embarrassing) when the branch is already on remote.
The straight-forward way out is to rename your local branch, delete the old branch on remote and then push your renamed branch. Although renaming your local branch is an inevitable step, there is a smarter way to deal with the remote one.
git push origin :[old] [new]
Pushing the old branch with a colon (:) followed by the new branch deletes the old one on remote and pushes the new one in a single shot.
“git log” — The registry
The git log
command is used for displaying the history of commits in a Git repository. It can be used to see the details of each commit, such as the author, timestamp, commit message, and changes made in the commit.
I use it mostly when I quickly need to get the ID of a commit. It’s a handy and easy to use tool.
git log
When the repository gets busy, the output of git log
can be long and overwhelming. For this reason, this command can be used with various options to filter by author, branch, date, file and almost anything you would need.
“git alias” — Let’s automate things!
It is a common situation in Git to do a certain task after another. For example, we commit after adding and pull after a checkout. While there’s nothing wrong with shooting multiple commands, it can be shortened and automated to a great extent.
git config --global alias.[name] "command(s) to execute"
Aliasing in Git refers to adding alternate names for your commonly used commands for easier access. Some examples can be:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
As you would expect, running git co master
would check out master for you. Same applies to the other mentioned commands.
This is fun, but not ‘automation’ as promised in the heading. To achieve this, we will be adding multiple commands to an alias.
git config --global alias.ready '!git add . && git commit'
This will add an alias which will add (changes as well as new files) and start a commit. Example use:
git ready -m 'commit message'
Conclusion
In conclusion, Git is a powerful tool for version control, and it is important to know more than just the basics of it. By incorporating these commands into your daily workflow, you can improve your skills and confidence in using Git. So take some time to explore and experiment with these commands and see how they can enhance your development experience. Happy coding!