简体   繁体   English

如何恢复被 git 删除的文件?

[英]How to restore files that were removed by git?

I am a git noob and git just deleted a bunch of important files.我是一个 git 菜鸟和 git 刚刚删除了一堆重要文件。 How do I get them back?我如何让他们回来?

I have a repo on my local machine.我在本地机器上有一个仓库。 To get into git, I just right click my project folder and select "git bash here".要进入 git,我只需右键单击我的项目文件夹和 select “git bash here”。 This brings up the master where I do all my giting.这带来了我做所有 giting 的主人。

So I had some changes to stage and I did:所以我对舞台进行了一些更改,我做到了:

git add .

This staged a bunch of changes.这上演了一系列变化。 I noticed that I didn't want some of these staged so I decided that I'd try to unstage everthing.我注意到我不希望其中一些上演,所以我决定尝试取消上演所有内容。 I did this:我这样做了:

git reset --hard HEAD^

This basically deleted a bunch of files that I had made on the last commit and jumped to the commit before.这基本上删除了我在上次提交时创建的一堆文件并跳转到之前的提交。

How do I get those files back?我如何取回这些文件? If I can't do it through git is there another way?如果我不能通过 git 做到这一点,还有其他方法吗?

I stand corrected regarding my original advice (see edit history).我对我的原始建议持正确态度(请参阅编辑历史记录)。 As penance, I've tracked down the simplest way to recover your lost files from git:作为忏悔,我已经找到了从 git 恢复丢失文件的最简单方法:

git fsck --lost-found
ls .git/lost-found/other

Unfortunately, the filenames are lost, but the contents should all be there as distinct files.不幸的是,文件名丢失了,但内容应该都作为不同的文件存在。

From this SO-Question :从这个SO问题


You can (with some work) recover state of file at the last "git add <file> ".您可以(通过一些工作)在最后一个“git add <file> ”恢复文件状态。 You can use您可以使用

$ git fsck --cached --no-reflogs --lost-found --unreachable HEAD

and then examine files in ' .git/lost-found/blob/ ' directory.然后检查“ .git/lost-found/blob/ ”目录中的文件。

Please read git fsck manpage: I have not checked above invocation.请阅读git fsck联机帮助页:我没有检查上面的调用。


It sounds like you want the old HEAD (as opposed to HEAD^), in which case you can simply check out that revision.听起来您想要旧的 HEAD(而不是 HEAD^),在这种情况下,您可以简单地查看该修订版。 Use git reflog to show the previous HEADs and look for the commit you want, then check it out.使用git reflog显示以前的 HEAD 并查找您想要的提交,然后查看它。

If you want the files that you staged before the reset, see the other answers.如果您想要在重置之前暂存的文件,请参阅其他答案。

I know this is an old thread, but I have to add something here.我知道这是一个旧线程,但我必须在这里添加一些内容。
after using使用后

$ git fsck --cache --no-reflogs --lost-found --unreachable HEAD

we get the list of thousands of files (like it was for me) and looks like it's no possible to check all ones manually so we can filter the list to find blob files only in any suitable way for you, in may case it was Python and I just copied the output of the git fsck from console to the file the read file to get a list of rows我们得到了数千个文件的列表(就像对我来说一样),看起来不可能手动检查所有文件,所以我们可以过滤列表以仅以任何适合您的方式查找 blob 文件,在可能的情况下它是 Python我刚刚将 git fsck 的 output 从控制台复制到文件读取文件以获取行列表

with open("lost_files.txt") as file:
    lost_files = file.readlines()

Then filter to separate blob ones然后过滤以分离 blob 的

[line.split(' ')[-1].strip() for line in lost_files if "blob" in line]

result was the list of hashes Then go to console and declare a variable FILES and past here the list of hashes generated above FILES=('5c2d8667ef8abbd7e114d2f9b61ee840b034e56f'....... '6dad86cd9c7a80ff5c3cd1d3222d2f8228dc18cf') for it was near 5К files then just write a simple loop in console result was the list of hashes Then go to console and declare a variable FILES and past here the list of hashes generated above FILES=('5c2d8667ef8abbd7e114d2f9b61ee840b034e56f'....... '6dad86cd9c7a80ff5c3cd1d3222d2f8228dc18cf') for it was near 5К files then just write控制台中的一个简单循环

for lost_file in "${FILES[@]}"; do   git show  $lost_file > lost/lost_file_$lost_file.txt; done

as result I get 5К files in lost directory Then it's need to simply open each file and grep data you want for me it was next script ( I try to use meaningful naming when working on features so I just add filter by keyword)结果我在丢失的目录中得到了 5К 文件然后它需要简单地打开每个文件和 grep 数据你想要我这是下一个脚本(我在处理功能时尝试使用有意义的命名,所以我只需按关键字添加过滤器)

import os

lost_files_names = os.listdir("lost")
names = []
for file in lost_files_names:
    with open(f"lost/{file}", "rb") as data:
        temp = data.readlines()
    for x in temp:
        if b"my_keyword" in x:
            names.append(file)
for x in set(names):
    print(x)
 

Finlay I have got output with 10 files and managed to restore stupidly lost uncommitted code (Thank god, I did git add) in a short amount of time Finlay 我有 10 个文件的 output 并设法在短时间内恢复愚蠢丢失的未提交代码(感谢上帝,我做了 git 添加)

No, with不,与

git reset --hard HEAD^

the changes are reverted back to the last commit and lost permanently.更改将恢复到上次提交并永久丢失。

As @tanascius mentioned, you can use the below command to restore added, but not committed files.正如@tanascius 所提到的,您可以使用以下命令来恢复添加但未提交的文件。 This command prints in console SHA-1 references to the blob and trees objects.此命令在控制台中打印对blob对象的 SHA-1 引用。

$ git fsck --cache --no-reflogs --lost-found --unreachable HEAD

The blob objects are exactly your files that were lost by git reset command. blob对象正是您被git reset命令丢失的文件。 Use the following command to restore it (actual filenames are lost, unfortunately):使用以下命令恢复它(不幸的是,实际文件名丢失了):

$ git show "<SHA-1 REFERENCE TO THE BLOB OBJECT HERE>" > lost_file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM