Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 931
lprsd Avatar asked Feb 23 '10 14:02

lprsd


People also ask

How do I undo a commit to a remote?

To undo the last commit from a remote git repository, you can use the git reset command. command. This will undo the last commit locally. command to force push the local commit which was reverted to the remote git repository.

How do you remove pushed commits from remote?

To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote. Notice the + sign before the name of the branch you are pushing, this tells git to force the push.

Can you undo a push in git?

We show three methods to undo pushed commits from a remote repository in Git. We use the git reset , revert , and checkout commands for this. When we use git reset , we also remove any trace of the unwanted commits from the repository history.


2 Answers

Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick -- the latter applies the patch to a branch that's missing it, the former removes it from a branch that has it.

like image 72
Andrew Aylett Avatar answered Oct 13 '22 10:10

Andrew Aylett


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> 
like image 34
Mulan Avatar answered Oct 13 '22 08:10

Mulan