Useful Git Commands That You Might Want (or Need) to Use in Your Project
At it’s most basic level, using Git is a matter of learning a handful of essential codes. As a version control, you can get by with just using commands like pull, add, commit, and push. However, Git is much more powerful than that. Once you’ve familiarized yourself with the basics of Git, you might want to dive deeper into other commands that Git provides to help you on your project.
Personally, I’ve been a part of multiple small to medium scale projects that utilizes Git, and I’ve found some useful Git commands that have previously helped me in my project. Here’s a list of a few of them:
- Cherry-Pick
Cherry pick is an interesting one. Although it’s not necessary an essential or vital command to use, it’s one that you might want to use to credit other people’s work. More specifically, cherry-pick is used to apply changes that were made in a specific commit, without applying any other changes made from commits before or after that specific commit.
Right now, I’m currently working on a new feature in one of my project’s PBI in the branch PBI-4.4. It’s going smoothly, but I haven’t necessarily finished my work yet. Then, one of my friends pushed a new feature to the IMPROVEMENT branch, which I’d like to implement into the current branch that I’m working on.
However, if I pull and merge the IMPROVEMENT branch into my current working branch, then I’d need to also bring in the changes that the previous commit before the specific commit I wanted brought. I don’t want this, since I’m only interested in the changes made from a specific commit.
Here, I can use the git cherry-pick command. With cherry-pick, I’ll only bring in changes that were made in that specific branch. Here, I want to cherry-pick the commit in the screenshot down below. To do so, I’d need to type in “git cherry-pick e4899dc40f16c2e618f47845506b2ef12a769d5e”, where the long string of numbers and letters is the commit hash. - Stash
Stash is a command that’s pretty similar to commit. As it name suggests, it stashes the changes that you’ve made so far, and then it reverts the working directory into the last commit. This is useful, for example, if you’ve made progress and changes on a feature and you want to check out a different branch but you don’t necessarily want to commit the changes you’ve made just yet.
For example, I was working on a new feature on a branch called “PBI-4.1". My progress is good, but it’s not quite there yet. Using commit now would not be optimal since I haven’t quite finished my progress. Then, something more urgent came up and I’d need to work on a different branch that is unrelated to the work that I’m currently doing on the branch “PBI-4.1”. I don’t want my progress to in this branch to carry over and affect the other, more urgent branch, so I used git add to save my progress and then used git checkout to go and work on the urgent branch. Then, I get hit with an error that you can see in image 2.1 down below.
The problem is quite obvious- when you check out another branch, git will load the work directory of that branch, and if you have changes made in your current branch, it would be overwritten and you would lose your progress in an instant. One of the ways you can fix this is by committing your changes, but we can also use stash to save our changes without having an uncomplete commit in our git log history. After using git stash, you’d see the message on image 2.2. Now, your changes have been saved (or stashed) and your current working directory is now set to the latest commit. I can now check out other branches and when I’m done, I can simply do “git stash apply” or “git stash pop” to restore the previously stashed changes. The difference between those 2 commands is that with “apply”, the stash entry itself is not deleted and you can apply the stashed changes again in the future, while with pop the stash entry is deleted. - Revert
This is one of the more important commands in this list. We are all susceptible to writing codes that has bugs or errors, and we are all susceptible to missing those bugs and errors. I’ve previously been in a situation where I’ve committed my work thinking that everything is going well and smoothly, only to later find out that that commit introduces a bug that ruins a lot of things. To fix it, I can just restore the version before the faulty commit using “git revert”. This command will create a new commit that applies changes which undoes the changes introduced in the last commit. This way, the faulty commit itself is still there in the commit history, but we have reverted the working files into the version before the faulty commit itself.
Obviously, Git offers so much more than just these 3 commands. However, these are commands that I’ve personally used and found useful multiple times in my projects. Learning these commands allows me to be more flexible and have a better confidence overall in using Git, and I hope that you also feel the same way after reading this blog.