3

Need some help!

  1. I added a few files using "git add "
  2. I wanted to checkout the other files and accidentally ran "git checkout -f"

I do know its possible to get these changes back using reflog etc, but I don't know how! I know it's possible to get a diff. I really need those changes.

Any ideas??

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
sr09
  • 720
  • 5
  • 26
  • 1
    You might try something like [this](http://stackoverflow.com/questions/7348698/git-how-to-list-all-objects-in-the-database) and see if you find something. However, Makoto is right, `-f` means "Trust me, I know what I'm doing". Don't just blindly add it to commands. – Roman May 19 '14 at 02:22
  • See also [How to recover after i execute :git reset --hard HEAD](http://stackoverflow.com/q/14251194/456814). –  May 19 '14 at 03:25
  • @R0MANARMY: I'm admittedly conflicted, since there was a solution for the OP found in `git fsck`. I don't see my answer being very viable, since there's a way to recover. – Makoto May 19 '14 at 03:25
  • Possible duplicate: [Recover files that were added to the index but then removed by a git reset](http://stackoverflow.com/q/10782978/456814). –  May 19 '14 at 03:26
  • Possible duplicate: [Can deleted files that are added but not committed in Git be recovered?](http://stackoverflow.com/q/12434618/456814). –  May 19 '14 at 03:29
  • Related: [Recovering added file after doing git reset --hard HEAD^](http://stackoverflow.com/q/1108853/456814) –  May 19 '14 at 03:30
  • 1
    Btw, nice work for figuring this out (more or less on your own) instead of just complaining that people are trying to close your question =). – Roman May 19 '14 at 03:58

1 Answers1

10

Okay, I fixed it. It is possible to recover files that have been added to the index. It is possible to recover the files only if they have either been added or stashed.

Here are the steps:

Step 1:

git fsck −−lost-found

This will give you a list of dangling blob IDs - the stuff you added and did not commit (before doing the force checkout)

Step 2:

git show <ID>

This is the ID associated with that dangling blob. It will spit the contents of the file to the console.

That's it!

This thread helped me: Recover from git reset --hard?

Community
  • 1
  • 1
sr09
  • 720
  • 5
  • 26
  • 2
    I'm actually really proud that you found this answer. I hadn't realized that there was an option with `git fsck`. I need to spend more time in the plumbing, apparently! – Makoto May 19 '14 at 03:26
  • 2
    @Makoto as soon as you `git add`, you make Git "aware" of the files, as demonstrated by the solution. This is actually a duplicate of other questions that have been asked, I'm just having a hard time finding a good canonical one, since it tends to pop up every now and again. –  May 19 '14 at 03:27
  • Then when you find the file with `git show`, how do you retrieve it? – young_souvlaki Nov 23 '20 at 17:45