0

Say I have a file file.txt, I've committed some revisions and now I've edited it in my working copy, I didn't stage or commit these edits.

Then I use git checkout <an-old-revision> file.txt, the file.txt goes back to an old commit.
Now I regret my checkout, how do I bring my edits back?

CarmeloS
  • 7,868
  • 8
  • 56
  • 103
  • possible duplicate of [Get back the changes after accidental checkout?](http://stackoverflow.com/questions/2961240/get-back-the-changes-after-accidental-checkout) – genisage Feb 06 '15 at 06:58

1 Answers1

3

With git you cannot get these local changes back because you never committed/added or stashed them. If you use an IDE like Eclipse or IntelliJ Idea then the first thing i would check is if you can recover your changes via the local history of the file. If you do not use an IDE then a backup would be helpful.

mithrandir
  • 738
  • 1
  • 6
  • 17
  • Is there any hack to avoid this scenario from happening? – CarmeloS Feb 06 '15 at 08:56
  • Normally git aborts a checkout operation if you have uncommited local changes that would get lost. You will see an error message: `error: Your local changes to the following files would be overwritten by checkout:` – mithrandir Feb 06 '15 at 09:44
  • the change is not staged and I didn't see the warning, my git version is "git version 1.9.3 (Apple Git-50)" – CarmeloS Feb 06 '15 at 09:45
  • 1
    You didn't see that error because you didn't checkout a branch. Rather you told git to reset the file.txt contents to the specific revision. As you never told git about a file change (`add`, `stash`, `commit`) it will happily overwrite file contents when running `git checkout `, making you lose your edits. – Alexander Groß Feb 06 '15 at 09:53