Git | Remove a Commit in the Middle of a Branch Using Rebase

Do you need a simple way to git remove a commit from anywhere in the branch? Do you like to keep your git history clean? Luckily with Git, there is always a way. Even if you’re not an advanced Git user, this trick should be fairly straightforward to implement. Don’t be intimidated! It utilizes git rebase, which rewrites your commit history. Basically, using rebase, you can remove any git commit from your history. The git rebase feature is used in many ways to rewrite history, including removing single commits from the middle of the branch.

What is git rebase?

Git rebase is a command that allows you to integrate changes from one branch into another while giving you more control over the history. It can also be used to rewrite a single branch (you don’t have to be merging in another branch). It’s used to keep a git history clean by modifying past commits that were already committed. Using rebase, you can easily use git to remove a commit in the middle of a branch.

The Command

git rebase -i <commit id>~1

To remove a commit, you need to get its commit ID. Once you are in interactive mode, which is what the -i flag is, you can remove anything that leads up to that commit. For example, if you select a commit that is 5 commits back in your git history, you will have the option to interact with the last 5 commits. In our case, we only want to git remove a commit from the middle.

In the picture above, I entered a4b47233db497c58a48ff47a7cf069f737d2bc35 in the git rebase command. Notice that the HEAD points to the top commit. That was my most recent commit at the time. In my case, I entered git rebase -i a4b47233db497c58a48ff47a7cf069f737d2bc35~1.

If you chose to read the text in the image above, you’ll notice that there are a lot of options when in interactive rebase mode. For our purposes, removing or commenting out the desired commits should be enough.

Notice that the HEAD commit is at the bottom and the commit that I entered in the command is at the top. If we’re only targeting a single commit, only a single comment is needed.

To remove any commit, comment out the line (put a # in front of it). You can remove as many commits as you want. In this case, I only want to remove a single commit in the middle of this branch. Once you’ve done that, save the revision (CTRL+X then press enter if you’re using the default nano editor).

Let’s check the git history again using git log.

Basically, I used git to remove a commit from history, like it was never there in the first place. A nice and clean solution, especially if you like to keep your git history clean.

Interactive Commits

Since this rebase is interactive using the -i flag, git will go through each commit that needs an action and you will run git rebase --continue when you are done with it. A common example is a merge conflict, which you will solve by manually editing the files, running git add . and then git rebase --continue. Once these commits are taken care of, your history will successfully be rewritten! In some cases, you may not need to do anything at all. Pretty simple, right?

When I ran into this issue on a project, some googling led me to this post.


  • Anonymous

    Posted on

    Excellent, thanks!

    • brandon parker

      Posted on

      You’re very welcome!