[git] Undo a particular commit in Git that's been pushed to remote repos

What is the simplest way to undo a particular commit that is:

  • not in the head or HEAD
  • Has been pushed to the remote.

Because if it is not the latest commit,

git reset HEAD

doesn't work. And because it has been pushed to a remote,

git rebase -i

and

git rebase --onto

will cause some problem in the remotes.

More so, I don't want to modify the history really. If there was bad code, it was there in the history and can be seen. I just want it out in the working copy, and I don't mind a reverse merge commit.

In other words, what is the Git equivalent of the following svn commands:

svn merge -r 303:295 http://svn.example.com/repos/calc/trunk

which removes all changes from 295 to 302 by reverse merging all changes in those revisions, as a new commit.

svn merge -c -302 ^/trunk

which undoes the 302 commit, of course by adding another commit that reverse merges the changes from that respective commit.

I thought it should be a fairly simple operation in Git and a fairly common use case. What else is the point of atomic commits?

We have staging stashing and all to ensure the commits are perfectly atomic, shouldn't you be able to undo one or more of those atomic commits easily?

This question is related to git version-control git-reset git-revert

The answer is


If the commit you want to revert is a merged commit (has been merged already), then you should either -m 1 or -m 2 option as shown below. This will let git know which parent commit of the merged commit to use. More details can be found HERE.

  • git revert <commit> -m 1
  • git revert <commit> -m 2

Because it has already been pushed, you shouldn't directly manipulate history. git revert will revert specific changes from a commit using a new commit, so as to not manipulate commit history.


I don't like the auto-commit that git revert does, so this might be helpful for some.

If you just want the modified files not the auto-commit, you can use --no-commit

% git revert --no-commit <commit hash>

which is the same as the -n

% git revert -n <commit hash>

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to version-control

How can I switch to another branch in git? Do I commit the package-lock.json file created by npm 5? Project vs Repository in GitHub Remove a modified file from pull request Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: How to squash all commits on branch git: updates were rejected because the remote contains work that you do not have locally Sourcetree - undo unpushed commits Cannot checkout, file is unmerged Git diff between current branch and master but not including unmerged master commits

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?

Examples related to git-revert

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits How to undo local changes to a specific file How do I use 'git reset --hard HEAD' to revert to a previous commit? What's the difference between Git Revert, Checkout and Reset? Why does git revert complain about a missing -m option? How to revert uncommitted changes including files and folders? Reset all changes after last commit in git How do I revert a Git repository to a previous commit? Undo a particular commit in Git that's been pushed to remote repos