从 git reset --hard 中恢复?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5788037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recover from git reset --hard?
提问by Jacob Lyles
Is there any way to recover uncommitted changes to the working directory from a git reset --hard HEAD
?
有什么方法可以从git reset --hard HEAD
?恢复对工作目录的未提交更改?
回答by sehe
You cannotget 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. (This writes the objects to the .git/lost-found/
directory; from there you can use git show <filename>
to see the contents of each file.)
先前暂存的更改 ( git add
) 应该可以从索引对象中恢复,因此如果您这样做了,请使用git fsck --lost-found
来定位与其相关的对象。(这会将对象写入.git/lost-found/
目录;您可以从那里git show <filename>
查看每个文件的内容。)
If not, the answer here would be: look at your backup. Perhaps your editor/IDE stores temp copiesunder /tmp or C:\TEMP and things like that.[1]
如果没有,这里的答案将是:查看您的备份。也许您的编辑器/IDE 将临时副本存储在 /tmp 或 C:\TEMP 下,等等。[1]
git reset HEAD@{1}
This will restore to the previous HEAD
这将恢复到以前的 HEAD
[1] vime.g. optionally stores persistent undo, eclipseIDE stores local history; such features might save your a**
[1] vimeg 可选地存储持久性撤消,eclipseIDE 存储本地历史;此类功能可能会节省您的**
回答by ken
answer from this SO
来自这个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! : )
你回来了!:)
回答by Justin
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 using git show <filename>
, copy out the blobs, and rename them.
我git reset --hard
今天也意外地运行了我的 repo,同时今天也有未提交的更改。为了取回它,我运行了git fsck --lost-found
,它将所有未引用的 blob 写入<path to repo>/.git/lost-found/
. 由于文件未提交,我other
在<path to repo>/.git/lost-found/
. 从那里,我可以使用 看到未提交的文件git show <filename>
,复制出 blob 并重命名它们。
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.
注意:这仅在您将要保存的文件添加到索引时才有效(使用git add .
)。如果文件不在索引中,它们就会丢失。
回答by Gabe
Yes, YOU CAN RECOVER from a hard resetin git.
是的,您可以从git 中的硬重置中恢复。
Use:
用:
git reflog
to get the identifier of your commit. Then use:
获取您提交的标识符。然后使用:
git reset --hard <commit-id-retrieved-using-reflog>
This trick saved my life a couple of times.
这个技巧救了我几次命。
You can find the documentation of reflog HERE.
您可以在此处找到 reflog 的文档。
回答by Orcun
While I was working on a local project, I wanted to move it to GitHub and then created a new repository. While I was trying to add all these files to the new repository with .gitignore, I accidentally added a wrong file and then tried to clear it.
当我在做一个本地项目时,我想把它移到 GitHub,然后创建一个新的存储库。当我尝试使用 .gitignore 将所有这些文件添加到新存储库时,我不小心添加了一个错误的文件,然后试图清除它。
I ran git reset --hard origin/master
:P
我跑了git reset --hard origin/master
:P
Then all of my local files deleted because the repo was empty. I thought everything was gone.
然后我所有的本地文件都被删除了,因为 repo 是空的。我以为一切都过去了。
This saved my life:
这救了我的命:
git reflog show
git reset HEAD@{1}
git push
Hope it saves another life.
希望它能挽救另一个生命。
回答by JonathanTien
If you use something like IntelliJ:
如果你使用像 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.
项目或文件夹的本地历史视图显示您在过去几天内所做的一切。在对话框下方的操作栏中,选择要回滚的操作。[...] 这样一来,对话框的上部显示了更改文件的树形视图。如果您只想恢复已删除的文件,而不管此后所做的其他更改,您可以在树视图中选择文件 Lost.txt 并单击“恢复”按钮。
http://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/
http://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/
This just got my arse out the fire!
这让我火冒三丈!
回答by user1147827
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.
我只是做了git reset --hard
并丢失了所有未提交的更改。幸运的是,我使用了一个编辑器 (IntelliJ),并且能够从本地历史记录中恢复更改。Eclipse 应该允许您这样做。
回答by Matthieu Moy
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).
根据定义,git reset --hard
将丢弃未提交的更改,而 Git 没有任何方法可以恢复它们(您的备份系统可能会有所帮助,但不会帮助 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:
实际上,极少数情况下git reset --hard
是一个好主意。在大多数情况下,有一个更安全的命令可以做同样的事情:
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 rungit gc
. If you're 99.9% sure you'll never need these changes back, thengit stash
is still your friend for the 0.1% case. If you're 100% sure, thengit 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, thengit reset --keep
is your friend. It will do the same thing asgit reset --hard
, but will notdiscard your local changes.If you want to do both, then
git stash && git reset --keep
is your friend.
如果您想丢弃未提交的更改,请使用
git stash
. 它将保留这些更改的备份,如果您运行git gc
. 如果您 99.9% 确定您永远不需要这些更改,那么git stash
在 0.1% 的情况下仍然是您的朋友。如果您 100% 确定,那么git stash
仍然是您的朋友,因为这些 100% 有测量误差 ;-)。如果你想
HEAD
在历史中移动你和当前分支的尖端,那么git reset --keep
就是你的朋友。它将做与 相同的事情git reset --hard
,但不会丢弃您的本地更改。如果你想两者兼而有之,那么
git stash && git reset --keep
就是你的朋友。
Teach your fingers not to use git reset --hard
, it will pay back one day.
教你的手指不要用git reset --hard
,总有一天会回报的。
回答by Edwin Ikechukwu Okonkwo
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
假设HEAD@{2}
是您希望返回的状态
回答by DragonKnight
This is what I usually do if I lose some changes.
如果我丢失了一些更改,我通常会这样做。
git reflog
git checkout <commit id> // now you are in where you want but you cannot push from detached branch to master
manually copy and paste changes from detached branch to master or working branch
git reset --hard HEAD // if needed
git add ... > git commit ... > git push ...
to move the pointer back to your previous commits but keeping the changes you made so far in your latest commits checkout git reset --soft dadada
将指针移回您之前的提交,但在最近的提交检出中保留您迄今为止所做的更改 git reset --soft dadada