1

For some bug,I execute "git checkout .".It revert all files modified.Can I rollback the operation? I just just revert the specified file modified.I lost the other files modified. What I can do to solved the problem?

John.W
  • 11
  • 3

2 Answers2

2

I think you have not added the working directory changes to index. Due to this, when you run below command,

git checkout . #. is pathspec, which corresponding to current directory

The whole contents of current directory are replaced by index. Your working changes are lost. Unless, you have some other regular backup mechanism, you cannot recover these lost files.

The git documentation talks about this:

git checkout [-f|--ours|--theirs|-m|--conflict=] [] [--] …​

git checkout [-f|--ours|--theirs|-m|--conflict=] [] --pathspec-from-file= [--pathspec-file-nul]

Overwrite the contents of the files that match the pathspec. When the (most often a commit) is not given, overwrite working tree with the contents in the index. When the is given, overwrite both the index and the working tree with the contents at the .

As a general best practice, don't run git checkout command against dirty working directory. add the changes to index and then go with git checkout. Read more about this problem and other best practices

Community
  • 1
  • 1
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
0

I'm not sure what you did, or how you did it... but "don't panic":

https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting

Checkout old commits

Since this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

So the first thing you want to do is check and see if you've "lost" any uncommitted changes. In all likelihood - you haven't.

But if worse comes to worst - and to answer your question - No, you can't "undo" a checkout

FoggyDay
  • 11,962
  • 4
  • 34
  • 48