Back after a while with another git magic. We already have seen how to get rid of the top most commit in this article -> Git magic - Make commits disappear
First of all, it's one of the not so recommended way of doing it and more than that, it can only get rid of sequential commits from latest. I recently happened to get into a scenario where I had to drop a commit in the history by keeping the latest ones. As part of that exploration, presenting you this article.
/**
* @disclaimer
* Please read this post fully before executing any command. My scenario might not be same as yours.
*/
To my greatest surprise, I didn't know a git rebase could do this. Please continue to read to know how. The steps are simple,
- Count until the commit line you need to drop using git log
- Do an interactive rebase and you're done.
f1be775 build: add wctr to mocha exclude and webpack include
It's 4th commit from the top and since we are dropping that commit, let's take the count as 5.
2. Do an interactive rebase
This is the last and final step where we will do an interactive rebase to drop a commit from the history.
> git rebase -i HEAD~5
In the command, the option -i indicates interactive rebase. Vim or any other assigned editor opens with the above content and if you carefully see, the chosen 5 commits are shown in the reverse order.
Now carefully decide what are all the commits you need to drop and change the command from pick to drop in the editor you see, like below.
Save and close the editor. At this point, you might get into situations where conflicts happen because of the commits you dropped.
At that point,
- Resolve conflicts
- Stage the changed files
- Do a git rebase --continue
Comments
Post a Comment