问题描述
我运行了以下命令:
git add .
由于文件很多(> 10TB),因此添加时间很长.中途我不小心删除了一些文件(需要恢复).因此,如果我在终端中执行"Ctrl + C"(中断Git).
git add .
The since there are many files (> 10TB), so it was taking time to add. Halfway through I accidentally deleted some files (which I need to recover). So, I if I do "Ctrl + C" in terminal (interrupting Git).
会发生什么?
- 将在git中进行部分添加(我可以使用git checkout恢复一些文件吗?)
- 不会在git中添加任何文件.
谢谢
推荐答案
在我的实验中,中断的git add
在索引中什么也没有留下(您可以使用git status
确认),但是它确实保留了所有斑点在其内部结构中创建的文件,因此可能有些文件可以恢复.看看 https://git-scm.com/book/zh-cn/v2/Git-Internals-Git-Objects ,了解有关从Git内部Blob中提取文件的信息.
In my experimentation, an interrupted git add
leaves nothing in the index (you can confirm this by using git status
), but it does keep any blobs it created in its internal structures, so there may be some files that can be recovered.Have a look at https://git-scm.com/book/en/v2/Git-Internals-Git-Objects for information on extracting files from Git's internal blobs.
我以这种方式恢复了一个文件:
I recovered one file this way:
> find .git/objects -type f
...
.git/objects/ac/a6e96aaf9492a2ee8f9ef51f0197ad56436fd4
...
> git cat-file -p aca6e96aaf9492a2ee8f9ef51f0197ad56436fd4 > file1
请注意,块ID是目录名ac
加上blob文件名a6e96...
来构成aca6e96...
.
Note that the bloc ID is the directory name ac
plus the blob file name a6e96...
to make aca6e96...
.
这样,Git给了我一个文件的内容.但是,这不会很有趣,因为您得到的文件内容中没有文件名.不幸的是,如果您有机会进行提交,则文件名将存储在索引中,并且存储在更持久的结构中,但是该信息尚不适用于git add
中断期间创建的Blob.
This way Git gave me the contents of one file. This is not going to be fun to use, though, because you get the file contents without the file name. Unfortunately, the file name would have been stored in the index, and in more durable structures if you had had a chance to do the commit, but that information would not be available yet for blobs created during an interrupted git add
.
以下是一个脚本,该脚本将使用分隔符在一个文件中列出所有blob,这可能会使您的生活更加轻松:
Here's a script that will list all your blobs in one file with separators, which might make your life a bit easier:
文件list-blobs.pl
:
#!/usr/bin/perl
open BLOBS, "find .git/objects -type f |";
while (<BLOBS>) {
chop;
s#.*(..)/#\1#;
print "BLOB $_\n";
system("git cat-file -p $_");
}
运行
chmod +x list-blobs.pl
list-blobs.pl | less
,您将看到Git在中断git add
之前实际存储在blob中的对象.
and you will see what objects Git has actually stored in blobs before you interrupted git add
.
更简单:使用 https://github.com/ethomson/git-recover 由其作者爱德华·汤姆森(Edward Thomson)在以下评论中分享.
Easier yet: use https://github.com/ethomson/git-recover shared by its author Edward Thomson in the comments below.
这篇关于如果我中断git add命令会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!