Recover from git reset

有没有办法从git reset --hard HEAD恢复未提交的工作目录更改?


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 eg optionally stores persistent undo, eclipse IDE stores local history; such features might save your a**


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.


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! : )

链接地址: http://www.djcxy.com/p/23492.html

上一篇: 意外地回到了主人,失去了未提交的变化

下一篇: 从git重置恢复