I deleted some files.
I did NOT commit yet.
I want to reset my workspace to recover the files.
I did a git checkout .
.
But the deleted files are still missing.
And git status
shows:
# On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: cc.properties # deleted: store/README # deleted: store/cc.properties #
Why doesn't git checkout .
reset the workspace to HEAD
?
You can restore a deleted file from a Git repository using the git checkout command. If you do not know when a file was last deleted, you can use git rev-list to find the checksum of the commit in which that file was deleted.
Recovering Deleted Files with the Command Line Recovering a deleted file using the Git command line involves the ` git restore ` or ` git checkout `command. Whenever you modify files in Git—including creating new files, editing, or deleting existing files—the changes start as unstaged.
In your case you must have used git rm to remove the file, which is equivalent to simply removing it with rm and then staging that change. If you first unstage it with git reset -- <file> you can then recover it with git checkout -- <file> .
The process for recovering a deleted commit is quite simple. All that is necessary is to use `git reflog` to find the SHA value of the commit that you want to go to, and then execute the `git reset --hard <sha value>` command.
The output tells you what you need to do. git reset HEAD cc.properties
etc.
This will unstage the rm operation. After that, running a git status
again will tell you that you need to do a git checkout -- cc.properties
to get the file back.
Update: I have this in my config file
$ git config alias.unstage reset HEAD
which I usually use to unstage stuff.
You've staged the deletion so you need to do:
git checkout HEAD cc.properties store/README store/cc.properties
git checkout .
only checks out from the index where the deletion has already been staged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With