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


当前回答

一般来说,您无法返回未提交的更改。

以前的分段更改(git add)应该可以从索引对象中恢复,所以如果你这样做了,使用git fsck——lost-found来定位与它相关的对象。(这将对象写到.git/lost-found/目录;从那里你可以使用git show <filename>来查看每个文件的内容。)

如果没有,答案是:看看你的备份。也许你的编辑器/IDE将临时拷贝存储在/tmp或C:\ temp之类的目录下

git reset HEAD@{1}

这将恢复到前一个HEAD

[1] vim例如,可选地存储持久撤销,eclipse IDE存储本地历史;这样的功能可能会节省你的**

其他回答

一般来说,您无法返回未提交的更改。

以前的分段更改(git add)应该可以从索引对象中恢复,所以如果你这样做了,使用git fsck——lost-found来定位与它相关的对象。(这将对象写到.git/lost-found/目录;从那里你可以使用git show <filename>来查看每个文件的内容。)

如果没有,答案是:看看你的备份。也许你的编辑器/IDE将临时拷贝存储在/tmp或C:\ temp之类的目录下

git reset HEAD@{1}

这将恢复到前一个HEAD

[1] vim例如,可选地存储持久撤销,eclipse IDE存储本地历史;这样的功能可能会节省你的**

根据定义,git reset -hard会丢弃未提交的更改,git无法恢复它们(你的备份系统可能会有帮助,但不是git)。

实际上,很少有git重置的情况——困难是个好主意。在大多数情况下,有一个更安全的命令来做同样的事情:

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.

教你的手指不要使用git重置——很难,总有一天会有回报的。

答案来自这个SO

$ git reflog show

4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app

# assuming you want to get back to 7c49ec7 (restore dependencies to the User model)

$ git reset HEAD@{2}

你的好日子回来了!:)

如果您正在Netbeans上开发,请查看文件选项卡和文件编辑区域之间的内容。这里有“来源”和“历史”。在“历史记录”中,你会看到使用版本控制(git/other)所做的更改,但也有本地所做的更改。在这种情况下,局部更改可以拯救您。

如果你幸运地有相同的文件打开在另一个编辑器(例如。Sublime Text)试试按ctrl-z键。它救了我。