0

I accidentally did checkout file. And that file had code changes.

So Can I recover that changes ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sandy
  • 256
  • 3
  • 5
  • How did you do the checkout such that you overwrote the file? Git would have warned you. – Dai Mar 23 '18 at 21:00
  • 2
    You mean you had *unstaged/uncommitted* changes to you local files that where overwritten by the git checkout (which most likely was a reset.)? Your best bet is your IDE which may have its own history of changes. – Timothy Truckle Mar 23 '18 at 21:03
  • Dai, while I was doing git checkout, git did't warn me, it just replaced server version file. – Sandy Apr 04 '18 at 14:14
  • Thanks Timothy for IDE idea, Actually I am working in linux without any IDE, I am using VIM for my work so I wouldn't have that approach, may be I would needing to do backup regularly for avoid this mistake. – Sandy Apr 04 '18 at 14:22

1 Answers1

1

Contrary to what people leaving comments would have you believe, a path-specific checkout (ie git checkout path/to/my/file) would indeed overwrite your unstaged changes in that file without warning, because that is exactly the command git expects you to use if you intend to undo unstaged changes.

And unfortunately, because the changes were unstaged, git has no history of them. While git is very good at retaining history once it's been committed, it offers basically no special protection for the work tree; so commands affecting untracked files or files with unstaged changes are particularly dangerous.

If you take continuous backups (or are lucky and took a backup while the changes existed), you could use those. Some IDE's keep a work history that might help. But unfortunately, in most cases (unless you have unusually solid backup practices) there probably isn't much to be done in this scenario.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Thanks Mark, for IDE idea. but as I say on previous comment I am using VIM for my development work. That is better that I would be needing to do backup regularly. So started using "git diff > mypatch-date.patch" daily basis to avoid this kind of messup. – Sandy Apr 04 '18 at 14:25