How to revert multiple git commits?

git revert range of commits
git revert multiple commits after push
git revert merge commit
git revert commit
git revert multiple merge commits
git revert --no-commit
git revert mainline
git revert pushed commit

I have a git repository that looks like this:

A -> B -> C -> D -> HEAD

I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A.

It sounds like I can either try to rebase (doesn't apply, since I've pushed changes in between), or revert. But how do I revert multiple commits? Do I revert one at a time? Is the order important?

Expanding what I wrote in a comment

The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.

So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.

You have the following situation:

A <-- B  <-- C <-- D                                               <-- master <-- HEAD

(arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).

What you need to create is the following:

A <-- B  <-- C <-- D <-- [(BCD)^-1]                   <-- master <-- HEAD

where "[(BCD)^-1]" means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)^-1 = D^-1 C^-1 B^-1, so you can get the required situation using the following commands:

$ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "the commit message"

Alternate solution would be to checkout contents of commit A, and commit this state:

$ git checkout -f A -- .
$ git commit -a

Then you would have the following situation:

A <-- B  <-- C <-- D <-- A'                       <-- master <-- HEAD

The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).

The solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset:

$ git reset --hard A
$ git reset --soft @{1}  # (or ORIG_HEAD), which is D
$ git commit

Git, git reset. The first method is a simple way to throw away few recent commits, it re-​writes the commit history, so only use it when your  I prefer to revert multiple commits by manually pasting in the commit ids in order from most recent commit to oldest commit. Look at the above image and watch how I get back to commit 1 below: git revert --no-commit most_recent_commit 2nd_most_recent_commit 3rd_most_recent etc..

For doing so you just have to use the revert command, specifying the range of commits you want to get reverted.

Taking into account your example, you'd have to do this (assuming you're on branch 'master'):

git revert master~3..master

This will create a new commit in your local with the inverse commit of B, C and D (meaning that it will undo changes introduced by these commits):

A <- B <- C <- D <- BCD' <- HEAD

git revert multiple commits - How to revert multiple git , The following command will revert the last 3 commits with only one commit. $ git revert --no-commit HEAD~3.. git revert If your changes are pushed to the remote repository or you want in general to aviod changing the commit history, then it is better to use revert. The revert command takes SHA1 of one or several commits and generates the new change to reverse the effect of these commits.

Clean way which I found useful

git revert --no-commit HEAD~3..

This command reverts last 3 commits with only one commit.

Also doesn't rewrite history.

Git: Revert one or more commits, git revert commit_sha2. To revert changes that are split across multiple commits, use the --no-commit flag. Copy. git revert --no-commit commit_sha3 git revert  git revert multiple commits with single command. Use grep, cut and xargs to revert multiple commits of a feature. Hello Folks, There are situations when we want to revert all the commits of a feature instead of checking out an earlier commit, because you want to retain the changes made for other features.

Similar to Jakub's answer, this allows you to easily select consecutive commits to revert.

# revert all commits from B to HEAD, inclusively
$ git revert --no-commit B..HEAD  
$ git commit -m 'message'

Git revert multiple commits || Ajit Singh, This requires your working tree to be clean (no modifications from the HEAD commit). Note: git revert is used to record some new commits to reverse the effect of  Alternative 1: git revert. First create a scratch branch at the point where you started your adventure. $ git checkout -b tmp-revert faada93. By specifying a commit range, git revert will undo multiple commits. $ git revert da8b496..faada93 Alternative 2: git commit-tree

git reset --hard a
git reset --mixed d
git commit

That will act as a revert for all of them at once. Give a good commit message.

git-revert Documentation, The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset , move the HEAD and  Hm, git revert definitely takes multiple commit arguments now, though it's a relatively recent addition - merged to master in 8c7da86, first contained in v1.7.2. – Cascabel Jan 17 '11 at 20:39 @Jefromi: thanks, I'm using an earlier version but should have thought to check git.git

Git Revert, In this quick tutorial we're going to look at rewinding “time” in Git. Say you've already made a commit and decide to go back to that commit… Well it's not too hard  If you want to revert commit range B to D (at least in git version 2) in a single commit, you can do. git revert -n B^..D This revert the changes done by commits from B's parent commit (excluded) to the D commit (included), but doesn't create any commit with the reverted changes.

How to go back multiple commits in Git with Git revert – TruthSeekers, You can use Git's powerful feature to revert any commit by clicking the Revert button in merge requests and commit details. Reverting a merge request. Note: The  Pull the latest version of your repository from Bitbucket using the git pull --all command. Run the Git log command with -n 4 from your terminal. The number after the -n determines the number of commits in the Reset the head of your repository's history using the git reset --hard HEAD~N where N

Reverting changes, Undoing Commits. When you want to undo a commit, you have two basic options: reset and revert. Note: Reset can only be performed on your HEAD branch. The following command will revert the last 3 commits with only one commit. $ git revert --no-commit HEAD~3..

Comments
  • If you just want to reset the remote, you can clobber it with anything! But let us use the fourth commit ago: git push -f HEAD~4:master (assuming the remote branch is master). Yes, you can push any commit like that.
  • If people have pulled you have to make a commit that reverts changes using git revert.
  • Use git show HEAD~4 to ensure you are pushing to right one to the remote
  • Possible duplicate of How to undo last commit(s) in Git?
  • "Is the order important?" Yes, if the commits affect the same lines in the same files. Then you should start reverting the most recent commit, and work your way back.
  • If you added files in B, C or D. the git checkout -f A -- . Will not delete these, you will have to do it manually. I applied this strategy now, thanks Jakub
  • Those solutions are not equivalent. The first one doesn't delete newly created files.
  • @Jerry: git checkout foo might mean checkout branch foo (switch to branch) or checkout file foo (from index). -- is used to disambiguate, e.g. git checkout -- foo is always about file.
  • In addition to great answer. This shorthand works for me git revert --no-commit D C B
  • @welldan97: Thanks for a comment. When writing this answer git revert didn't accept multiple commits; it is quite new addition.