Undo a git commit

22,877

Solution 1

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

Solution 2

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

Share:
22,877
edwardmlyte
Author by

edwardmlyte

Updated on July 09, 2022

Comments

  • edwardmlyte
    edwardmlyte almost 2 years

    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?

  • eis
    eis over 11 years
    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.
  • automaticAllDramatic
    automaticAllDramatic over 11 years
    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.
  • cmp
    cmp over 8 years
    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.
  • edwardmlyte
    edwardmlyte over 8 years
    Does the answer need updating to SOFT? Also, refer to stackoverflow.com/a/21778/509213 to recover from an unwanted HARD reset.