Removing commit in between on Git
February 15, 2022
Problem
Let’s suppose you have a repository with the following commit history:
9649a8b (HEAD -> main) third commit
d28d4b8 second commit
2c28acd first commit
You want to remove only the second commit (d28d4b8
), leaving only the
first and the third ones untouched
Solution
Simply run a interactive rebase, passing as argument the hash of the commit to
be removed (with a ^
):
git rebase -i f318ad8^
The
^
character will extend the rebase range to include the offending commit
Now that the interactive rebase is listing both the second and the third commits, simply erase the line containing the commit to be removed, save and quit, example below:
pick d28d4b8 second commit # erase this line
pick 9649a8b third commit