How to undo 'git checkout -f' to revert uncommitted changes

I accidentally typed git checkout -f

because I was trying to recover a deleted file but now all uncommitted files are gone ... what a day of work ... Is there a way to get it back? Thank.

+3


source to share


3 answers


Do you have a backup system that tracks file changes? You could get it from there. If not, you're out of luck - commit more often!



+2


source


You have a chance for snow sitting outside Dante Inferno, but it depends on one important step .

You must run git add

for these files at some point before this.

Otherwise, you are not to have a good time.

If you have one, you can run git fsck --lost-found

to recover the files you deleted. What you get is not the exact filename, but the associated blob commit.



makoto@LATLON-Undefined:~/Desktop/smoketest$ echo "Goodbye file" > badfile.txt
makoto@LATLON-Undefined:~/Desktop/smoketest$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    badfile.txt

nothing added to commit but untracked files present (use "git add" to track)
makoto@LATLON-Undefined:~/Desktop/smoketest$ git add .
makoto@LATLON-Undefined:~/Desktop/smoketest$ git reset --hard HEAD
HEAD is now at 7124f25 Initial
makoto@LATLON-Undefined:~/Desktop/smoketest$ git status
On branch master
nothing to commit, working directory clean
makoto@LATLON-Undefined:~/Desktop/smoketest$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
dangling blob 4a503984f4847abd45f4d795d1b74c6482164dcb

      

None of these blocks have file information attached to them; namely, what is the name. But they are still your data.

Depending on what you have on hand, you can do one of two things:

  • Enter in each dangling blob and manually try to get the filename for it, or
  • While referring to this site , you can experiment with your Bash script to pull out all the droplets and place them in your current directory with their hash as the filename.

    I have not personally tested this script, but it does show how it works.

    for blob in $(git fsck --lost-found | awk '$2 == "blob" { print $3 }');
        do git cat-file -p $blob > $blob.txt;
    done
    
          

+2


source


Two options:

  • Check git stash to see if you have any previously saved files:

    git stash list
    git stash show stash@{0}
    
          

  • If you've added / delivered files earlier, you can try git fsck --lost-found

    . git will keep all the dangling blobs in the directory ./git/lost-found/other

    (although they won't have their original filenames).

0


source







All Articles