Reputation: 16597
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?
Upvotes: 60
Views: 49625
Reputation: 2063
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
Upvotes: 38
Reputation: 393694
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
Upvotes: 66