How do you remove a specific revision in the git history?
Suppose your git history looks like this:
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?
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
A range of commits could also be
removed with rebase. If we have the
then the command
git rebase --onto topicA~5 topicA~3 topicA
would result in the removal of
commits F and G:
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
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:
- there is some embarrassing commit that you wish never existed
- there are ZERO subsequent commits that actually depend on that embarassing commit (zero conflicts on revert)
- you don’t care that you will be listed as the ‘Committer’ of the hundreds of intervening commits (‘Author’ will be preserved)
- 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:
git branch base B
git branch remove-me R
git branch save
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
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
reset --hard, and
cherry-pick. And you can delete the
So here is the scenario that I faced, and how I solved it.
[branch-a] [Hundreds of commits] -> [R] -> [I]
R is the commit that I needed to be removed, and
I is a single commit that comes after
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