See how to remove commits from the Git history like a pro by using the powerful interactive rebase method. This tutorial assumes you have basic experience with Git.
Sometimes you can make a commit in git and you might later reconsider. Perhaps you made a commit that contains code changes that you do not need anymore or you commited something by mistake. You can try to fix things manually on your branch and make another commit without these changes but this will leave you with a messy commit history. A lot of organizations prefer to keep a clean git chronology and will ask you as a developer to keep only “valuable commits” and remove any commits that are not necessary. Clean Git history makes our lives as developers much more easier because it makes the code much easier to read and maintain. However, if you did not know how to delete a specific commit in Git and your boss asks you to do it (like in my situation), this can make your life not so easier as you would want it to be. That is why, I decided to share my experience, so you do not have to struggle the way I did it the first time. Before showing you how to remove a specific commit from the Git history with interactive rebase and vim, here is a quick refreshment in case you need it.
What is interactive rebase?
Interactive rebase in Git is a powerful tool that provides you with more manual control over the history revision process in Git. It is especially useful when you want to remove, add or edit a specific commit, as well as combining multiple commits into one bigger commit.
What is commit history?
This is pretty self-explanatory but basically it shows a list of commits in a tree-like structure. The easiest way to see the commit history is by using the git log command in git bash:
git log --oneline
This command will give you a list with all commit hashes, starting from the most recent one. (Do not panich if you are unsure how to exit the screen, you can safely do it by typing “q”, which stands for quit in vim)
What is vim?
Vim is the default text editor in Git Bash. This tutorial assumes that you are using vim as a Git Bash editor. Vim is far from being an intuitive text editor. However, it does a good job and it is particularly useful in interactive rebase operations, i.e. removing unnecessary commits from the Git history.
So far, so good. In the above screenshot, we have just seen the last 5 commits that we have made in Git. Now, let’s say we want to remove the fourth most recent commit with hash 6ebf351. To do that, add the following line in Git bash:
git rebase -i HEAD~4
HEAD~4 points to the 4th latest commit compared to the branch head. Change the number with the commit that you would like to see removed from the history.
The above command should bring you to the vim interface and you will see a list with the last four commits.
In vim, press esc to use the editing mode, then select the line you want to edit, then press dd to delete the line. Finally, save and exit, this time using :wq command.
After you have done the rebase, do not forget to push your changes:
git push origin +branch
Finally, check your history with Git log and the fourth latest commit should no longer be visible.