Recover from git reset --hard?

2018-12-31 10:29发布

Is there any way to recover uncommitted changes to the working directory from a git reset --hard HEAD?

标签: git
19条回答
人间绝色
2楼-- · 2018-12-31 10:46

You cannot get back uncommitted changes in general.

Previously staged changes (git add) should be recoverable from index objects, so if you did, use git fsck --lost-found to locate the objects related to it.

If not, the answer here would be: look at your backup. Perhaps your editor/IDE stores temp copies under /tmp or C:\TEMP and things like that.[1]

git reset HEAD@{1}

This will restore to the previous HEAD

[1] vim e.g. optionally stores persistent undo, eclipse IDE stores local history; such features might save your a**

查看更多
像晚风撩人
3楼-- · 2018-12-31 10:47

answer from this SO

$ git reflog show
93567ad HEAD@{0}: reset: moving to HEAD@{6}    
203e84e HEAD@{1}: reset: moving to HEAD@{1}    
9937a76 HEAD@{2}: reset: moving to HEAD@{2}
203e84e HEAD@{3}: checkout: moving from master to master
203e84e HEAD@{4}: reset: moving to HEAD~1
9937a76 HEAD@{5}: reset: moving to HEAD~1
d5bb59f HEAD@{6}: reset: moving to HEAD~1
9300f9d HEAD@{7}: commit: fix-bug

# said the commit to be recovered back is on 9300f9d (with commit message fix-bug)
$ git reset HEAD@{7}

You got your day back! : )

查看更多
余欢
4楼-- · 2018-12-31 10:48

I accidentally ran git reset --hard on my repo today too while having uncommitted changes too today. To get it back, I ran git fsck --lost-found, which wrote all unreferenced blobs to <path to repo>/.git/lost-found/. Since the files were uncommitted, I found them in the other directory within the <path to repo>/.git/lost-found/. From there, I can see the uncommitted files, copy out the blobs, and rename them.

Note: This only works if you added the files you want to save to the index (using git add .). If the files weren't in the index, they are lost.

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 10:48

if you accidentally hard reset a commit, then do this,

git reflog show
git reset HEAD@{2} // i.e where HEAD used to be two moves ago - may be different for your case

assuming HEAD@{2} is the state you desire to go back to

查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 10:50

By definition, git reset --hard will throw away uncommitted changes without any way for Git to recover them (your backup system may help, but not Git).

Actually, there are very few cases where git reset --hard is a good idea. In most cases, there's a safer command to do the same thing:

  • If you want to throw away your uncommitted changes, then use git stash. It will keep a backup of these changes, which will expire after some time if you run git gc. If you're 99.9% sure you'll never need these changes back, then git stash is still your friend for the 0.1% case. If you're 100% sure, then git stash is still your friend because these 100% have a measurement error ;-).

  • If you want to move your HEAD and the tip of the current branch in history, then git reset --keep is your friend. It will do the same thing as git reset --hard, but will not discard your local changes.

  • If you want to do both, then git stash && git reset --keep is your friend.

Teach your fingers not to use git reset --hard, it will pay back one day.

查看更多
伤终究还是伤i
7楼-- · 2018-12-31 10:50

The information is lost.

Since you did not commit, your .git never stored this information. So, basically git cannot recover it for you.

But, If you just did git diff, there is a way you can recover using the terminal output with the following 3 simple steps.

  1. scroll your terminal and look for the o/p of git diff. Save the o/p in a file called diff.patch
  2. Search & Replace all 7 spaces and 8 spaces with tab(\t) character and save the changes.
  3. Go into your git repository. Apply the diff.patch (patch -p1 < diff.patch)

You are saved! :)

Note : While you are copying the data from terminal to a file, be careful and clearly see that the data is continuous output and did not contain any redundant data(due to pressing up and down arrows). Otherwise you might mess it up.

查看更多
登录 后发表回答