Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if i interrupt git add command?

Tags:

git

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?

  1. There will be partial add in git (and I can recover some files using git checkout?)
  2. There won't be any files added in git.

Thanks,

like image 646
nilesh Avatar asked Jan 17 '19 13:01

nilesh


People also ask

What happens if git push is interrupted?

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.

Can you skip git add?

@uvuv - no, you cannot.

How do I undo a git add command?

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.

What happens if you give the command git add?

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.


1 Answers

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.

like image 108
joanis Avatar answered Nov 15 '22 01:11

joanis