Imad
Imad

Reputation: 7490

Retrieve deleted file from GIT

I am new to git. I have stashed my changed before starting to work on other branch. When I was done with that I came back and saw my stash. One new file is missing from the stash that I added, rest of the files are there. I also deleted that file from Changes since I though it is safe in stash. Is there any way to get that file back?

Upvotes: 1

Views: 258

Answers (1)

farhad
farhad

Reputation: 430

If you've never added that file, it's almost gone but if you have, here is a weird way to find it.

Run this command where your '.git' folder is.

find .git/objects | sed -n 's_.git/objects/\(.\{2\}\)/\(.*\)_\1\2_p' | xargs -n1 git cat-file -p | grep "content" -A 20 -B 20

And Replace content with something that you know it was in your file content.

Hopefully, You'll find that file then you can change -A and -B params to print the whole file.

Explanation:

  • First, we extract all git objects files with find.
  • Then we generate the hash name of the object with sed
  • Then we print out the content of every object using xargs and git cat-file and search for the blob containing the file we want.

Upvotes: 8

Related Questions