1

I just did:

git checkout -- .

without realizing that there were important, non-committed changes. I have not touched the repo since making that mistake.

Is there any way of "undoing" that command, or have I lost the uncommitted work?

Em Edih
  • 23
  • 3
  • This is same as `git checkout .` If you are using an editor like IntelliJ, there's an option to view local history where you can find your changes. – Sid Nov 06 '18 at 08:57
  • Since you never told git about these changes, there's no way to get them back from git, unfortunately. – OhleC Nov 06 '18 at 09:00
  • @ohlec tehnically I did tell git: the moment I did `git checkout -- .`, `git` *could* have decided to keep a copy. – Em Edih Nov 06 '18 at 09:02
  • Git does not have code that makes a copy of the files, other than committing them (staging them also makes a temporary copy into the index). That checkout command overwrote modified files and there is, git-wise, no way to get them back. If you have added new files, those should still be present, but those that were modified were unfortunately overwritten, your changes to those are unfortunately lost. – Lasse V. Karlsen Nov 06 '18 at 09:07
  • 1
    @Sid I am using PyCharm and it has a similar option. Thanks! – Em Edih Nov 06 '18 at 09:08
  • @LasseVågsætherKarlsen I see. I was hoping that, similar to the *staging area* and the *stashing area*, `git` was keeping a separate *undo buffer*. It seems there is no such a thing. – Em Edih Nov 06 '18 at 09:11
  • Unfortunately not, you can create aliases that does something though, but out of the box there is nothing of the sorts. – Lasse V. Karlsen Nov 06 '18 at 09:25
  • Possible duplicate of [Can git undo a checkout of unstaged files](https://stackoverflow.com/questions/2689265/can-git-undo-a-checkout-of-unstaged-files) – phd Nov 06 '18 at 09:26
  • https://stackoverflow.com/search?q=%5Bgit%5D+undo+checkout – phd Nov 06 '18 at 09:26

2 Answers2

1

You can revert git checkout if and only if you have done one of these two:

  1. Used git stash to record the previous state of the working directory (before git checkout). You can revert git checkout by using git stash apply or git stash drop
  2. Used an IDE that keeps track of file history. You can revert by using the revert feature of that particular IDE.

Otherwise, unfortunately there is no way to revert uncommitted changes removed by git checkout.

Andreas
  • 2,455
  • 10
  • 21
  • 24
  • The second option does work for me. It is not git related, but I am using PyCharm and it has indeed kept track of all changes, even uncommitted ones, and even the ones performed outside the editor. – Em Edih Nov 06 '18 at 09:07
  • The only git-related solution is using `git stash`. Otherwise, you would have to resort to using other backup features (revert feature in IDE, OS file backup, etc) – Andreas Nov 06 '18 at 09:09
0

This is same as git checkout .

If you haven't stored the changes anywhere they are as good as lost.

If you are using an editor like IntelliJ, there's an option to view local history where you can find your changes

Sid
  • 4,893
  • 14
  • 55
  • 110