How to delete a only a specific commit in the middle of the git log

I want only to delete a specific commit in the middle of a git log. It shouldn't be affected to above commits.


If the commit were not pushed to a remote server, you can use git rebase to modify the git history.

You can do that:

git rebase -i commit-hash^

It will show a list of commits. The first line should be the commit you want to delete. Remove that line from the file you edited, save and close.

If you did push to a remote server prior to do that, note that the commits after the commit you just deleted were rewritten based on the commit before the one you just deleted from the history. For that particular reason, all of the commits are changed because your history just diverged. So if you try to push that, it won't be fast-forward and you'll have to force the push.

For that reason, if you're not familiar with git rebase I suggest keeping a branch pointing to the branch you were modifying the history in case something goes wrong.

Also, if that particular commit is shared accross multiple branches, diverging the history of one branch will cause multiple problems as you won't be able to easily merge one branch from the old history to the branch in the new history. It will duplicate many commit and the commit you deleted in one branch won't be deleted in the others.

Think of it has modifying git history is like time travel. Any change at one point create a new parallel universe. Everything that was after the change in the past really is impacted by the change you did in the past.


It shouldn't be affected to above commits.

If you remove a commit from the middle of a branch, or really anywhere except possibly the HEAD of a branch, you will rewrite the history of that branch. This is because the tools you would use to effect this, namely git rebase -interactive and git filter-branch, would need to recommit everything which comes after the commit you removed. This is usually not desirable for a public shared branch because it forces everyone to take pretty draconian measures to keep up with that change.

Rather, a safer bet for you would be to use git revert:

git revert <SHA-1 of commit to delete>

This will add a new commit on the top of your branch which effectively undoes everything which the middle commit did.

By the way, if you really want to remove that commit, I would recommend doing an interactive rebase.