Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore git files deleted after git merge --abort

I've lost files added to git during "merge conflict" phase.

Step by step:

git pull
git status

Git informs me about "merge conflict", that's okay. Then I create a new file and add it to git.

vi test.txt
git add test.txt

After that, abort merge:

git merge --abort

I have not found the file "test.txt" neither in directory, nor via "git fsck", nor "git reflog". Is it possible to restore the file?

like image 741
Alexander Ishmuradov Avatar asked Sep 13 '16 10:09

Alexander Ishmuradov


People also ask

How do I undo a merge abortion?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

Can you restore deleted file in git?

If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.

What happens if I abort merge?

So the “git merge –abort” operation essentially terminates the merger that you have just carried out and separated the two versions of your file, i.e., the current version and the older version.


1 Answers

What have you tried with git fsck ?

see this SO question : Recover files that were added to the index but then removed by a git reset


The general answer is : the sequence of instructions you typed has removed the file from tracking (and from the disk), there is no guarantee that the content of your file can be retrieved.

However, git has a lot of safety mechanism, and one of them is : if some data is entered somewhere in the repo, it will not be deleted before two weeks.

(git has a garbage collection mechanism, see git help gc)


If you did indeed run git add test.txt at some point, and this action was recent enough, there should still be some trace of the content of the file stored within git :

git fsck --full --unreachable --no-reflog

in git parlance, a file is a blob :

git fsck --full --unreachable --no-reflog | grep blob

This should give you a list of internal git hashes :

unreachable blob 08bf360988858a012dab3af4e0b0ea5f370a2ae8
unreachable blob 21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89
unreachable blob 08c12ef37075732cf4645269ab5687ba6ba68943
...

note that git add file.txt stores the file's content, not the file's name ...

If you remember a specific string in your file, you can try to narrow your research by using git grep <string> <hash> :

$ git fsck --full --unreachable --no-reflog | grep blob | awk '{ print $3 }' > list.txt
$ cat list.txt | while read blob; do
  if git grep -q "string" $blob; then
    echo $blob
  fi
done

You can then view the whole content of a blob using :

git show $blob

and hopefully find back the file you were looking for.

like image 166
LeGEC Avatar answered Oct 03 '22 05:10

LeGEC