在 Git 中,如何恢复在提交之前恢复的暂存文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11094968/
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
In Git, how can I recover a staged file that was reverted prior to committing?
提问by David
I was attempting to pull a change into my repository using Git Tower. When I did so, there was a conflict and I mistakenly hit stage all (as I wanted to commit after resolving the conflict). When I did so, the conflict marked itself as resolved.
我试图使用 Git Tower 将更改拉入我的存储库。当我这样做时,发生了冲突,我错误地点击了所有阶段(因为我想在解决冲突后提交)。当我这样做时,冲突将自己标记为已解决。
I wanted to manually resolve the change so I hit "Abort Merge", however, when I did this, It rolled back all my changes! Is there any way to get them back?
我想手动解决更改,所以我点击了“中止合并”,但是,当我这样做时,它回滚了我的所有更改!有没有办法让他们回来?
回答by Alexander Gladysh
If you had anything staged to git, you probably should be able to get that back. (If you just changed working copy, you wouldn't be able to restore it.)
如果你有任何东西上演到 git,你可能应该能够把它拿回来。(如果您只是更改了工作副本,您将无法恢复它。)
First of all: do notrun git gc
. Backup your repository and working copy before going ahead. (Make sure to backup .git
directory.) Also avoid closing terminal where this happened, and/or rebooting —?if all fails, you have a chance to find stuff in history / memory.
首先:你不运行git gc
。在继续之前备份您的存储库和工作副本。(确保备份.git
目录。)还要避免关闭发生这种情况的终端,和/或重新启动——如果一切都失败了,你有机会在历史/内存中找到东西。
Anyway, first thing to try is:
无论如何,首先要尝试的是:
git fsck --lost-found
It will print something like
它会打印类似的东西
Checking object directories: 100% (256/256), done. Checking objects: 100% (30165/30165), done. dangling blob 8f72c7d79f964b8279da93ca8c05bd685e892756 dangling commit 4993502a6394491190d3f4d6fb3d1e14019c2e9b
Since you lost staged files and did not do a commit, you're interested in dangling blob
entries.
由于您丢失了暂存文件并且没有进行提交,因此您对dangling blob
条目感兴趣。
Run git show <sha>
for each one — some of them should be your files.
git show <sha>
为每个运行- 其中一些应该是您的文件。
回答by Edward Thomson
To expand on Alexander's answer with a simpler alternative: yes, if you've staged your changes then you can probably get your files back. When you run git add
, files are actually added to Git's object database. At the moment that you do, git will put the file in the index:
用更简单的替代方案扩展亚历山大的答案:是的,如果您已经进行了更改,那么您可能可以取回文件。当您运行 时git add
,文件实际上被添加到 Git 的对象数据库中。此时,git 会将文件放入索引中:
% git add bar.txt
% git ls-files --stage
100644 ce013625030ba8dba906f756967f9e9ca394464a 0 bar.txt
100644 6af0abcdfc7822d5f87315af1bb3367484ee3c0c 0 foo.txt
Note that the entry for bar.txt contains the object ID of the file. Git has actually added the file to its object database. In this case, Git has added it to the repository as a loose object:
请注意 bar.txt 的条目包含文件的对象 ID。Git 实际上已将该文件添加到其对象数据库中。在这种情况下,Git 已将其作为松散对象添加到存储库中:
% ls -Flas .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
4 -r--r--r-- 1 ethomson staff 21 14 Jun 23:58 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
These files will eventuallybe garbage collected (so indeed, do not explicitly run git gc
). Thankfully, by default, this will happen in a matter of months, not days. Until these files are garbage collected, you can recover them.
这些文件最终将被垃圾收集(因此确实不要显式运行git gc
)。值得庆幸的是,默认情况下,这将在几个月内发生,而不是几天。在这些文件被垃圾收集之前,您可以恢复它们。
The easiest way to do this is to download and install the git-recover
programin interactive mode:
最简单的方法是在交互模式下下载并安装git-recover
程序:
% git recover -i
Recoverable orphaned git blobs:
61c2562a7b851b69596f0bcad1d8f54c400be977 (Thu 15 Jun 2017 12:20:22 CEST)
> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
> veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
> commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
Recover this file? [y,n,v,f,q,?]:
git-recover
looks for files in the object database that are notcommitted (or in the index). You can find out more about git-recover
in the blog post announcing it.
git-recover
在对象数据库中查找未提交(或在索引中)的文件。您可以git-recover
在宣布它的博客文章中找到更多信息。
回答by Philippe
A very late answer that could simplify (a lot? comparing to other answers) the recover by letting you figure it out quicker which blobs should be recovered:
一个很晚的答案,可以简化(很多?与其他答案相比)恢复,让您更快地找出应该恢复哪些 blob:
git fsck --full --no-reflogs --unreachable --lost-found | grep blob | cut -d\ -f3 | while read in; do printf "blob: $in\n"; git cat-file -p $in; printf "\n--------------------------------\n"; done > recover.txt
This command will create a file containing the hash and content of all the blobs that could be recovered.
此命令将创建一个文件,其中包含可以恢复的所有 blob 的散列和内容。
That way, you could easily search into this file and recover the blobs you want with the given hash (with the command git cat-file -p 8f72c7d79f964b8279da93ca8c05bd685e892756 > myFile.txt
)
这样,您可以轻松搜索此文件并使用给定的散列(使用命令git cat-file -p 8f72c7d79f964b8279da93ca8c05bd685e892756 > myFile.txt
)恢复您想要的 blob
Disclaimer: this file could become huge and slow to create if you've got a lot of unreachable blobs.
免责声明:如果您有很多无法访问的 blob,此文件可能会变得很大且创建速度很慢。