I ran the following command:
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).
What happens?
Thanks,
The upstream Git repository will be oblivious to your attempted push, and no change will occur upstream. Unfortunately however, as it doesn't do anything with the half-pushed files, it doesn't store it and then expect a continuation of the push later on either.
@uvuv - no, you cannot.
To undo git add before a commit, run git reset <file> or git reset to unstage all changes. You can read more about other commonly used Git actions in these helpful articles: Git checkout.
It only adds the content of the specified file(s) at the time the add command is run; if you want subsequent changes included in the next commit, then you must run git add again to add the new content to the index.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With