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
Note that the bloc ID is the directory name ac
plus the blob file name a6e96...
to make aca6e96...
.
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
.
Here's a script that will list all your blobs in one file with separators, which might make your life a bit easier:
File 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 $_");
}
Run
chmod +x list-blobs.pl
list-blobs.pl | less
and you will see what objects Git has actually stored in blobs before you interrupted git add
.
Easier yet: use https://github.com/ethomson/git-recover shared by its author Edward Thomson in the comments below.