7

Accidentally, I did git checkout -- . and lost one week of work. I tried git reflog, but there isn't a ref point with the unstagged changes that I wiped out.

Is it possible to undo git checkout -- . (git checkout double dash)

What about using git object to recover such things? Does git take snapshots of the repository even if there wasn't a git commit?

0x90
  • 39,472
  • 36
  • 165
  • 245
  • 3
    If you lost one week's work because of this, that must surely mean you hadn't committed for a week. If you hadn't committed, it makes sense there's nothing in `git reflog`. What *did* you do? Did you ever add anything to the index? –  Aug 17 '17 at 04:03
  • 2
    That won't wipe out any commits at all, only changes that you *haven't* committed. Did you go a week without committing? – hobbs Aug 17 '17 at 04:04
  • @hvd, yes it's was an hacking session of large project and I was on the fun and switched shells and I got a signal to call it a day :( always commit – 0x90 Aug 17 '17 at 04:07
  • 3
    No, you can’t recover from this with Git, unless maybe you used `git stash` at some point or you created lots of new files, staged, and unstaged them (unlikely). Commit more often and set up backups in the future. – Ry- Aug 17 '17 at 04:18
  • That's why snapshoting your system is sometimes handy. – 0x90 Aug 17 '17 at 04:24
  • I asked specifically if you ever added anything to the index, because if you did, it's possible (but difficult) to recover bits and pieces. You didn't answer that question. –  Aug 17 '17 at 04:25
  • @hvd it's the Linux kernel project, I did do other commits, but then I changed a lot of files just to tweak and see how it responses. I want to recover unstaged changes. So I have added 2 different commits before, and then did a lot of changes without doing git add. – 0x90 Aug 17 '17 at 04:27
  • Yeah, sorry, if you never ran any Git command, you're out of luck then as far as Git is concerned, it won't know about anything in those files... And if you had something outside of Git that tracked it, you'd be the one to know of it already. –  Aug 17 '17 at 04:30
  • 1
    Sort of a duplicate, but not exactly, of https://stackoverflow.com/questions/25791533/unstaged-files-gone-after-git-reset-hard/25792257 -- the question is different, but the problem is the same and the answer is as detailed as possible about possible recovery steps. Unfortunately, the accepted answer there agrees that you're out of luck. –  Aug 17 '17 at 04:34
  • @hvd yes that's what I thought, but it's good to have this as a reference here, might be useful for others. Feel free to edit it as answer and I'll accept it. Might help for others in the future. – 0x90 Aug 17 '17 at 04:36

1 Answers1

0

Unfortunately : the content of your files has to somehow land in git if you want to retrieve your files.

git checkout -- . is one of the few destructive commands (along with git reset --hard and git clean -f), which can remove content from your disk without storing it in git beforehand.

So git alone will not help you there ; you would have to turn to your IDE's history, or file recovery utilities.


For the record, here are actions that somehow "store content" in your repository :

  • git commit, obviously ...
  • git stash : this command actually creates complete commits, the last one you created will not expire, previous stashes will expire according to reflog expiry rules (default retention is 30 days, see help on git gc)
  • git add [file] (even without running git commit) : this writes the content of the file somewhere in git's database ; the file will not be stored along with its name but you can resort to some tricks to scan the files (blobs in git parlance) by their content. The default retention period for "loose files" is 2 weeks (see help on git gc)
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • @0x90 : technically, modifying file `a` then running `git checkout -- a` can be considered a noop - no pun intended ... – LeGEC Jun 28 '20 at 22:27
  • 1
    Not related to git or the question itself, BUT I'm on a mac and my local Time Machine snapshot saved my ass. If you ended up here, think of other backups you might have. Good luck! – Bugs Bunny Apr 14 '21 at 02:25