85

I added a new file F1 and made changes to another file F2 but then did a git reset --hard HEAD^ and I have lost all the changes to the files.

Is there some way, I can get them back?

I did look at a related question here: How can I undo git reset --hard HEAD~1? but, that question assumes that the one has done a Git commit.

2

6 Answers 6

169

You can (with some work) recover state of file at the last "git add <file>". You can use

$ git fsck --cache --no-reflogs --lost-found --dangling HEAD

and then examine files in '.git/lost-found/other' directory.

Please read git fsck manpage.

7
  • 11
    git show -p --format=raw $blob > $blob.txt +1, saved my life. Mar 17, 2014 at 16:43
  • 15
    It seems that on git 1.9.1, --unreachable causes .git/lost-found to not be created
    – canton7
    Apr 7, 2014 at 13:20
  • 52
    As stated --unreachable prevents creation of ".git/lost-found". I made it work using the following instead $ git fsck --cache --no-reflogs --lost-found --dangling HEAD Mar 3, 2015 at 14:37
  • 3
    this appears to have worked but i lost all the file extensions? Dec 13, 2015 at 21:11
  • 1
    Sorry, once I recovered the blob files, what to do next? May 31, 2016 at 1:10
40

(I'm assuming that the missing file is not part of any commit. Otherwise, git log --all -g --diff-filter=D --stat is your friend.)

  1. Get list of unreachable files that git knows a file name:

    git fsck --unreachable --no-reflogs --no-cache HEAD | fgrep " tree " \
    | cut -d " " -f3 | xargs -r -n1 git ls-tree \
    | fgrep " blob " | cut -d " " -f 3- | sort -k2 -u
    
  2. If you see something interesting, git cat-file blob SHA-1-of-interesting-file will output the file to standard output. (Example: git cat-file blob b8f0bdf56 > recovered-logo.png)

Unfortunately, if the missing file is not part of the any commit, git does not have a timestamp and as such, you cannot print various versions of files ordered by time.

If the missing file has never been staged (git stage or git add) or stashed (git stash), you're pretty much out of luck because as far as git knows, the file never did exist. (You may still try doing a git fsck --no-reflogs --lost-found and looking around in directory .git/lost-found/other to see if you have anything worth keeping in case git indeed has a copy of your missing file by some lucky accident. You do not have file names to help you in this case, only file contents.)

In case you just lost some commits (instead of just files), you'll probably want to run something like this:

gitk --all $( git fsck | awk '/dangling commit/ {print $3}'; git log -g --pretty='format:%H' )

That will run gitk with all the branches, all the reflog and all the dangling commits. You may want to add -n 10000 or some other limit in case your repo has really many commits (say linux kernel). If you do not have gitk, you may instead run lesser version using only command line like this:

git log --all --decorate --stat --graph --date-order $( git fsck | awk '/dangling commit/ {print $3}'; git log -g --pretty='format:%H' )

or a version with less verbose output

git log --all --decorate --oneline --graph --date-order $( git fsck | awk '/dangling commit/ {print $3}'; git log -g --pretty='format:%H' )

If you see some commit you want to save as branch recovered1, simply do git checkout -b recovered1 <sha1-of-the-commit>.

3
  • 2
    Wow! just found an untracked file, which got accidentally discarded by me (without even committing it), using git fsck --no-reflogs --lost-found and then searching it inside the directory .git/lost-found/other (with Notepade++'s Find in Files search). Thank you!
    – Ofir
    Nov 25, 2018 at 11:48
  • I have to add that I seriously recommend doing automated versioned backups over trying to recover files from git history. Mar 11, 2021 at 12:07
  • omg thank you! @Ofir, the same worked for me!! "$ git fsck --no-reflogs --lost-found" then search through files in ".git/lost-found/other"
    – greenhouse
    Jun 12, 2021 at 0:27
7

Try this http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html

I got a heart attack for the changes I lost. But after following this post. I got my changes back

2
  • You wel come :) Glad it helped. Mar 26, 2015 at 19:20
  • This answer just saved me 6 years later, THANKS !
    – Bryan Lee
    Sep 23, 2019 at 9:55
6

There is a git plugin that does this out of the box:

https://github.com/pendashteh/git-recover-index

$ cd /path/to/disatered/repo
$ git clone git@github.com:pendashteh/git-recover-index.git $HOME/.git-recover-index
$ $HOME/.git-recover-index/git-recover-index.sh
4

Actually, if you've added the object to the index (by using git add), there is a blob created for that state of the object - but there is no tree (and thus, commit) object that is referring to it. This is how one gets a 'dangling' loose object file, and if you run git fsck it will show you the unreferenced blob (git gc will delete these types of objects if it is run).

Because of this, you can use the reflog, if you have it enabled, to try and restore the index state for your file F1 that has been added. If you haven't added F2 at all, then as Greg said, git doesn't know anything about it and you're out of luck there.

0

If you use the VSCode, you can use the Timelime feature (by clicking right button mouse on changed file and choose "Open Timeline")

New contributor
John Fry is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.