60

Can you undo a past commit, that has long been merged into your git repository, via a git command? Or do you have to manually undo all the changes made in that commit?

asked Oct 3, 2012 at 8:26
3
  • 1
    Have a look on git commit --amend to modifiy the last commit or git revert where you can set the id of the commit you want to roll back. Youn can locate the commit's id with git log Git Amend Git Revert Commented Oct 3, 2012 at 8:28
  • Does this answer your question? How do I undo the most recent local commits in Git? Commented Aug 16, 2022 at 21:02
  • 1
    @DamianPavlica this question is specifically about an old commit which has had lots built on top of it. That answer you suggested is for the latest commit Commented Aug 17, 2022 at 23:25

2 Answers 2

65

You can always just revert the changes from a single commit by doing:

git revert <commit-id>

note that this creates a new commit, undoing just those changes

E.g. git log --oneline

d806fc9 two
18cdfa2 bye
62c332e hello
c7811ee initial

Say I want to revert changes in commit 18cdfa2:

git revert 18cdfa2

We now have: git log -1 -p

commit fb9d6627a71636503fc9a1eb4f725937c783ecee
Author: Seth <sehe@mint12.(none)>
Date: Wed Oct 3 10:32:46 2012 +0200
 Revert "bye"
 This reverts commit 18cdfa27c964b66b624be1030250757b745d6866.
diff --git a/a b/a
index 0907563..3b18e51 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-bye world
+hello world
answered Oct 3, 2012 at 8:31
Sign up to request clarification or add additional context in comments.

4 Comments

Attention: This also undoes all changes done on the file system which have been there before the unwanted commit.
@FranzHolzinger what do you mean? It doesn't in principle live example. A conflicting worktree edit will prevent revert: conflict error. Already committed conflicts will require manual conflict resolution as with any merge like this. Perhaps depending on configuration it can appear to lose local changes - if you have advanced features like autostash, rerere enabled?
This is not about a conflict. But I have made local changes on the file. Then I did a wrong commit. But when undoing the last commit, also my changes on the files are reverted to the version from Git without any warning.
@FranzHolzinger I showed you that's not what will happen. No doubt some other factor (a git reset or checkout maybe) was involved.
39

You can always do git revert <commit-hash> to undo a git commit. However, this in most cases is not helpful because it creates a new commit adding to your git reflog.

What I usually do when I have to step back is reset my branch to an earlier stage. To do this, do a git reflog and look for the HEAD position you want to move to.

rizwan@dragonstone:~/myrepo$ git reflog -3
8d386b8 HEAD@{0}: commit: I did something I don't want
2a59955 HEAD@{1}: commit: Feedback from PR #792
1023102 HEAD@{2}: checkout: moving from one branch to another

Now, say I want to move to commit hash 2a59955 and undo all the changes in 8d386b8. I would do:

Update: git reset --soft HEAD@{1} 

This will reset my branch to the initial state. Doing this will not only undo all the changes, but also stops tracking all the files that you might have added in this commit. By all means, this is the most efficient way to undo all the changes in a single commit.

This will undo all your changes. There is a hard reset meant to go back in time (at least from a git point of view). Use --hard in place of --soft for this

answered Oct 3, 2012 at 9:11

4 Comments

I would argue that it is helpful partly due to the fact that it creates a new commit. What if you want to undo your undoing? What if you want to be aware later that you've done reverting? also, one should mention committing+pushing, too, as just resetting doesn't change the commit history as such.
Yes, it does not and I agree that it is important to have a history of reverted commits too. Resetting helps when you want to move a step back and start over from the last state. Also, resetting stays in your logs and it not like you completely erase the commit.
SOFT! You use SOFT! You LOSE your changes with that command. I was looking for how to undo a commit which committed too much (damn, intellij) so here's a warning for the next guy.
Does the answer need updating to SOFT? Also, refer to stackoverflow.com/a/21778/509213 to recover from an unwanted HARD reset.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.