What's the simplest way to undo:
git reset HEAD~
Currently, the only way I can think of is doing a git clone http://...
from a remote repo.
Join Stack Overflow to learn, share knowledge, and build your career.
What's the simplest way to undo:
git reset HEAD~
Currently, the only way I can think of is doing a git clone http://...
from a remote repo.
git reset 'HEAD@{1}'
Git keeps a log of all ref updates (e.g., checkout, reset, commit, merge). You can view it by typing:
git reflog
Somewhere in this list is the commit that you lost. Let's say you just typed git reset HEAD~
and want to undo it. My reflog looks like this:
$ git reflog
3f6db14 HEAD@{0}: HEAD~: updating HEAD
d27924e HEAD@{1}: checkout: moving from d27924e0fe16776f0d0f1ee2933a0334a4787b4c
[...]
The first line says that HEAD
0 positions ago (in other words, the current position) is 3f6db14; it was obtained by resetting to HEAD~
. The second line says that HEAD
1 position ago (in other words, the state before the reset) is d27924e. It was obtained by checking out a particular commit (though that's not important right now). So, to undo the reset, run git reset HEAD@{1}
(or git reset d27924e
).
If, on the other hand, you've run some other commands since then that update HEAD, the commit you want won't be at the top of the list, and you'll need to search through the reflog
.
One final note: It may be easier to look at the reflog
for the specific branch you want to un-reset, say master, rather than HEAD
:
$ git reflog show master
c24138b master@{0}: merge origin/master: Fast-forward
90a2bf9 master@{1}: merge origin/master: Fast-forward
[...]
This should have less noise it in than the general HEAD reflog
.
reflog
for this purpose, I like to use git log --graph --decorate --oneline $(git rev-list -g --all)
. It shows a tree of all commits, including dangling unnamed branches
– texasflood
Jun 22 '15 at 18:02
git init
, and then right after that git reset --hard
. There is no reflog, or even any logs to begin with. Are these files pretty much toast now?
– AlanSE
Jul 27 '15 at 16:24
git add
(i.e. have never staged that version of the file), then git checkout
or git reset --hard
will blow away the only copy (the working directory) with no backup. I wish it didn't.
– Mark Lodato
Jul 28 '15 at 17:52
git reset HEAD~12
oops... git reset HEAD@{12}
nooo .. 'git reflog' to the rescue! oh it's just git reset HEAD@{1}
– Dennis
Sep 16 '15 at 19:35
Old question, and the posted answers work great. I'll chime in with another option though.
git reset ORIG_HEAD
ORIG_HEAD
references the commit that HEAD
previously referenced.
git reset ORIG_HEAD
? Thanks.
– Benjohn
Sep 3 '18 at 12:21
My situation was slightly different, I did git reset HEAD~
three times.
To undo it I had to do
git reset HEAD@{3}
so you should be able to do
git reset HEAD@{N}
But if you have done git reset using
git reset HEAD~3
you will need to do
git reset HEAD@{1}
{N} represents the number of operations in reflog, as Mark pointed out in the comments.
reflog
command provides you with. Agreed that your point about the numbers being the same for either direction ~3 / @{3} but didn't really need to be a new answer.
– Clint
Sep 3 '14 at 16:01
1.Use git reflog
to get all references update.
2.git reset <id_of_commit_to_which_you_want_restore>
--hard
! The risk of accidentally running this command is much higher. For instance, you want to unstage a single file withgit reset foo-file
. You only write the first part of the filename, hit tab for autocompletion, it actually completes to a branch name, you don’t notice it and run the commandgit reset foo-branch
instead. Voilà – Yushin Washio Jul 1 '19 at 11:16