How do you remove a specific revision in the git history?

Suppose your git history looks like this:


  • zsh tilde (~) expands differently on OS X
  • How to push changes to Git repository from uninitialized directory
  • git ignoring .gitattributes pattern
  • How to update a file in remote repo, without cloning that repo first?
  • Git for Windows using Beyond Compare
  • git multiple project merging
  • 1–5 are separate revisions. You need to remove 3 while still keeping 1, 2, 4 and 5. How can this be done?

    Is there an efficient method when there are hundreds of revisions after the one to be deleted?

  • Work-around for failing “git svn clone” (requiring full history)
  • How to select the appropriate diff /patch for a commit with rugged
  • Git-Flow undo a finished feature branch
  • git aws.push: No module named boto
  • Git remote server Push fails
  • Deploying an MVC4 C# application to Azure via GitHub. What should be in my .gitignore?
  • 8 Solutions collect form web for “How do you remove a specific revision in the git history?”

    To combine revision 3 and 4 into a single revision, you can use git rebase. If you want to remove the changes in revision 3, you need to use the edit command in the interactive rebase mode. If you want to combine the changes into a single revision, use squash.

    I have successfully used this squash technique, but have never needed to remove a revision before. The git-rebase documentation under “Splitting commits” should hopefully give you enough of an idea to figure it out. (Or someone else might know).

    From the git documentation:

    Start it with the oldest commit you want to retain as-is:

    git rebase -i <after-this-commit>

    An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart’s content, and you can remove them. The list looks more or less like this:

    pick deadbee The oneline of this commit
    pick fa1afe1 The oneline of the next commit

    The oneline descriptions are purely for your pleasure; git-rebase will not look at them but at the commit names (“deadbee” and “fa1afe1” in this example), so do not delete or edit the names.

    By replacing the command “pick” with the command “edit”, you can tell git-rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing.

    If you want to fold two or more commits into one, replace the command “pick” with “squash” for the second and subsequent commit. If the commits had different authors, it will attribute the squashed commit to the author of the first commit.

    Here is a way to remove non-interactively a specific <commit-id>, knowing only the <commit-id> you would like to remove:

    git rebase --onto <commit-id>^ <commit-id> HEAD

    Per this comment (and I checked that this is true), rado’s answer is very close but leaves git in a detached head state. Instead, remove HEAD and use this to remove <commit-id> from the branch you’re on:

    git rebase --onto <commit-id>^ <commit-id>

    As noted before git-rebase(1) is your friend. Assuming the commits are in your master branch, you would do:

    git rebase --onto master~3 master~2 master


    1---2---3---4---5  master


    1---2---4'---5' master

    From git-rebase(1):

    A range of commits could also be
    removed with rebase. If we have the
    following situation:

    E---F---G---H---I---J  topicA

    then the command

    git rebase --onto topicA~5 topicA~3 topicA

    would result in the removal of
    commits F and G:

    E---H'---I'---J'  topicA

    This is useful if F and G were flawed in some
    way, or should not be part of topicA.
    Note that the argument to –onto and
    the parameter can be any
    valid commit-ish.

    If all you want to do is remove the changes made in revision 3, you might want to use git revert.

    Git revert simply creates a new revision with changes that undo all of the changes in the revision you are reverting.

    What this means, is that you retain information about both the unwanted commit, and the commit that removes those changes.

    This is probably a lot more friendly if it’s at all possible the someone has pulled from your repository in the mean time, since the revert is basically just a standard commit.

    All the answers so far don’t address the trailing concern:

    Is there an efficient method when there are hundreds of revisions
    after the one to be deleted?

    The steps follow, but for reference, let’s assume the following history:

    [master] -> [hundreds-of-commits-including-merges] -> [C] -> [R] -> [B]

    commit just following the commit to be removed (clean)

    The commit to be removed

    commit just preceding the commit to be removed (base)

    Because of the “hundreds of revisions” constraint, I’m assuming the following pre-conditions:

    1. there is some embarrassing commit that you wish never existed
    2. there are ZERO subsequent commits that actually depend on that embarassing commit (zero conflicts on revert)
    3. you don’t care that you will be listed as the ‘Committer’ of the hundreds of intervening commits (‘Author’ will be preserved)
    4. you have never shared the repository
      • or you actually have enough influence over all the people who have ever cloned history with that commit in it to convince them to use your new history
      • and you don’t care about rewriting history

    This is a pretty restrictive set of constraints, but there is an interesting answer that actually works in this corner case.

    Here are the steps:

    1. git branch base B
    2. git branch remove-me R
    3. git branch save
    4. git rebase --preserve-merges --onto base remove-me

    If there are truly no conflicts, then this should proceed with no further interruptions. If there are conflicts, you can resolve them and rebase --continue or decide to just live with the embarrassment and rebase --abort.

    Now you should be on master that no longer has commit R in it. The save branch points to where you were before, in case you want to reconcile.

    How you want to arrange everyone else’s transfer over to your new history is up to you. You will need to be acquainted with stash, reset --hard, and cherry-pick. And you can delete the base, remove-me, and save branches

    So here is the scenario that I faced, and how I solved it.

    [Hundreds of commits] -> [R] -> [I]

    here R is the commit that I needed to be removed, and I is a single commit that comes after R

    I made a revert commit and squashed them together

    git revert [commit id of R]
    git rebase -i HEAD~3

    During the interactive rebase squash the last 2 commits.

    I also landed in a similar situation. Use interactive rebase using the command below and while selecting, drop 3rd commit.

    git rebase -i remote/branch
    Git Baby is a git and github fan, let's start git clone.