Is there any way to undo the effects of "git revert head"?
Asked Answered
P

4

67

I've accidentally run the command against the wrong branch in my repository - is there a way to undo this change?

Padron answered 7/9, 2010 at 20:40 Comment(1)
After reading the documentation for reset based on the advice below, git reset --hard head~1 solved it for me.Padron
I
98

git revert just creates a new commit -- you can "remove" it with git reset --hard HEAD^ (be more careful with it, though!)

Imbecilic answered 7/9, 2010 at 20:41 Comment(2)
Be careful with git reset --hard HEAD^ as it will remove any uncommited changes.Bantu
If you do a git stash before git reset --hard HEAD^ you can "save" your uncommited changes. After the git reset --hard HEAD^ do a git stash pop to reload the uncommited changes into the current branch.Algid
H
29

The command git revert just creates a commit that undoes another. You should be able to run git revert HEAD again and it'll undo your previous undo and add another commit for that. Or you could do git reset --hard HEAD~. But be careful with that last one as it erases data.

HEAD~ means the commit before the current HEAD

Hansen answered 7/9, 2010 at 20:43 Comment(1)
No, it does not erase data. It just moves your branch pointer. The previous commit still exists, and you can see its ID by looking at git reflog (f.ex.). It will get garbage-collected in two months (default configuration), but you can turn off automatic garbage collection, and then every single commit you ever made will exist forever in that repository. They just aren't reachable via branches. But you can always find them using git fsck, and I’ve posted a recipe for browsing all commits that uses that.Xanthochroid
A
4

How about reverting the revert?

View git log and get the hash tag of the bad revert:

git log -5

Then do reverse the revert itself:

git revert

Amply answered 25/9, 2012 at 11:9 Comment(1)
-1 this will create another commit, which is not really wanted hereTao
D
0

If you were prescient enough to have done this: revert --no-commit master, you can abort that with: git revert --abort per the git status advice:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
You are currently reverting commit dcc7c46.
  (all conflicts fixed: run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)
Drafty answered 24/3, 2018 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.