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:59

If you are trying to use the code below:

git reflog show
# head to recover to
git reset HEAD@{1} 

and for some reason are getting:

error: unknown switch `e'

then try wrapping HEAD@{1} in quotes

git reset 'HEAD@{1}'
查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 11:04

You can get back a commit after doing a reset --hard HEAD.

Make use of "git reflog" to check the history of the HEAD in the branch.

You will see your commit and its id here.

Do a

git reset {commit Id of the commit you want to bring back}
查看更多
怪性笑人.
4楼-- · 2018-12-31 11:05

I just did git reset --hard and lost all my uncommitted changes. Luckily, I use an editor (IntelliJ) and I was able to recover the changes from the Local History. Eclipse should allow you to do the same.

查看更多
其实,你不懂
5楼-- · 2018-12-31 11:07

If you're developing on Netbeans, look between the file tabs and the file edit area. There is a "Source" and "History". On "History" you'll see changes made using version control (git/other), but also changes made locally. In this case, local changes could save you.

查看更多
公子世无双
6楼-- · 2018-12-31 11:08

If you use something like IntelliJ:

On the context menu, choose Local History, and click Show History on the submenu:

The local history view for a project or folder shows you everything that you have done during the last few days. In the Action column of the lower part of the dialog box, select the action you want to roll back. [...] So doing, the upper part of the dialog box shows the tree view of changed files. If you want to restore the deleted file only, regardless of the other changes that have been done since then, you can select the file Lost.txt in the tree view and click the Revert button.

http://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/

This just got my arse out the fire!

查看更多
笑指拈花
7楼-- · 2018-12-31 11:11

Correct answers. OK, now I like git. :-) Here's a simpler recipe.

git log HEAD@{2}
git reset --hard  HEAD@{2}

Where "2" is the number of back to where you committed your changes. In my case, interrupted by colleague and boss to help debug some build issue; so, did a reset --hard twice; so, HEAD and HEAD@{1} were over-writes. Whew, would have lost an our of hard work.

查看更多
登录 后发表回答