In this article, we will see how to remove a specific commit at the middle in git.
Please make sure the changes are purely local and not yet pushed to remote. If the changes are already pushed to the remote then there might be a chances of other users might have already synced those commits. In such a scenario removing those commit will leads to unexpected situations.
Let us begin the process of removing the commit in middle
Identify the commit hash
First, use the git log
command to find the hash of the commit that we want to remove.
Note the commit hash that comes immediately before the commit you want to remove, as we need it for the next step.
git log --oneline
Start interactive rebase
Use the git rebase -i
command followed by the commit hash we noted in the previous step. This command will open an editor with a list of commits starting from the one you specified.
git rebase -i <commit-hash>
Remove the commit
In the editor, locate the commit(s) you want to remove. To remove the commit, delete the corresponding line or replace the word “pick” with “drop“. Finally, save the changes and exit the editor by pressing CTRL + X
.
pick e4cc5cfe c3
pick 1dea3078 c4
Git will apply the changes that we made in the commit and remove the specific commit.
if there are any conflicts appear during the process then git will prompt us to resolve them before continuing the rebase.
Once the conflicts are resolved, use the following command to continue rebase.
git rebase --continue