Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git recover deleted file where no commit was made after the delete

Tags:

git

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?

like image 650
Homan Avatar asked Aug 14 '12 16:08

Homan


People also ask

Can git restore deleted files?

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.

How do I undo a deleted file in git?

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.

How do I Unstage deleted files?

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> .

How do you get back the deleted commit?

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.


2 Answers

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.

like image 164
Noufal Ibrahim Avatar answered Sep 28 '22 00:09

Noufal Ibrahim


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.

like image 32
CB Bailey Avatar answered Sep 28 '22 00:09

CB Bailey