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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

Just do git checkout path/to/file-I-want-to-bring-back.txt

Solution 4 - Git

To recover all unstaged deletions at once, automatically, without specifying each single path:

git ls-files -z -d | xargs -0 git checkout --

To recover all staged deletions at once, automatically, without specifying each single path:

git status | grep 'deleted:' | awk '{print $2}' | xargs git checkout --

Solution 5 - Git

Since you're doing a git checkout ., it looks like you are trying to restore your branch back to the last commit state.

You can achieve this with a git reset HEAD --hard

Warning

Doing this may remove all your latest modifications and unstage your modifications, e.g., you can lose work. It may be what you want, but check out the docs to make sure.

Solution 6 - Git

if you used

git rm filename

to delete a file then

git checkout path/to/filename

doesn't work, so in that case

git checkout HEAD^ path/to/filename

should work

Solution 7 - Git

Here is the command that helped me on my mac. I tried a few of the other solutions but they did not work for me.

Git version on OSX Mavericks

mac-pro:main chris$ git version
git version 1.8.5.2 (Apple Git-48)

Command

git checkout HEAD -- path/to/file/file.cc

Solution 8 - Git

Here are different cases as a reference to help others:

If the deletion has not been committed, the command below will restore the deleted file in the working tree.

$ git checkout -- <file>

You can get a list of all the deleted files in the working tree using the command below.

$ git ls-files --deleted

If the deletion has been committed, find the commit where it happened, then recover the file from this commit.

#find the commit hash where it had this file deleted
$ git rev-list -n 1 HEAD -- <file>

It should give you something like c46e81aa403ecb8a0f7a323a358068345, now use this commit hash with the parent operator (^) like so:

$ git checkout <commit>^ -- <file>

Example:

$ git checkout c46e81aa403ecb8a0f7a323a358068345^ -- <file> 

In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.

$ git log --diff-filter=D --summary

If you want to just display the list of files:

git log --diff-filter=D --summary | grep "delete mode"

Solution 9 - Git

If you want to restore all of the files at once

Remember to use the period because it tells git to grab all of the files.

This command will reset the head and unstage all of the changes:

$ git reset HEAD . 

Then run this to restore all of the files:

$ git checkout .

Then doing a git status, you'll get:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Solution 10 - Git

git checkout HEAD -- client/src/pp_web/index.cljs

Solution 11 - Git

Use git ls-files to checkout deleted(-d) or modified(-m) files.

git checkout $(git ls-files -d)

see https://stackoverflow.com/questions/7415098/how-can-i-restore-only-the-modified-files-on-a-git-checkout

Solution 12 - Git

Do you can want see this

that goes for cases where you used

git checkout -- .

before you commit something.

You may also want to get rid of created files that have not yet been created. And you do not want them. With :

git reset -- .

Solution 13 - Git

If you have not committed any changes all you have to do is stash those changes and you will be back to the last working commit.

git stash
git stash clear
git clean 

Solution 14 - Git

Found this post while looking for answers on how to un-delete a file that was deleted in my working directory after a merge from another's branch. No commit was yet made after the merge. Since it was a merge in progress, i could not just add it back using:

$ git reset <commitid#-where-file.cpp-existed> file.cpp

I had to do another step in addition to the reset to bring the file back:

$ git checkout -- file.cpp

Solution 15 - Git

This was the easiest way for me:

git checkout HEAD~1 path/to/myfile.rb

I found it here.


Another way that also worked for me:

git reset HEAD path/to/myfile.rb
git restore path/to/myfile.rb

Solution 16 - Git

if you are looking for a deleted directory.

 git checkout ./pathToDir/*

Solution 17 - Git

Newer git (mine is 2.27.0) is more friendly and the actual commands are shown during "git status". For example, if you deleted the file tf.c, then

$ git status
...
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
      deleted:    tf.c

You would use "git restore tf.c" to get it back, as it saz. No more search!

Solution 18 - Git

For me what worked was git checkout {SHA1 of commit with version to restore} "{path to file to restore}"

For example git checkout 5a6b3179e58edff9c90326b9a04284b02fd67bd0 "src-ui/views/includes/radar.pug"

(executed in the branch that we want the file to go into)

After that command executes, the restored file will exist in the original location (which will need to be comited)

Solution 19 - Git

1.Find that particular commit to which you want to revert using:

   git log
This command will give you a list of commits done by you .

2.Revert to that commit using :

    git revert <commit id> 

Now you local branch would have all files in particular

Solution 20 - Git

If you have installed ToroiseGIT then just select "Revert..." menu item for parent folder popup-menu.

Solution 21 - Git

CAUTION: commit any work you wish to retain first.

You may reset your workspace (and recover the deleted files)

git checkout ./*

Solution 22 - Git

I happened to move (instead of copy) some json files from one folder to another within the same repository. Then I renamed those files and changed some contents in the new location. However I quickly learned that I have not copied and totally deleted the files from previous location.

Easy Solution:

git reset HEAD <oldfilepath_which_was_moved>
git restore <oldfilepath_which_was_moved>

Did this for all the files and they are back.

You can also include multiple files separated by space.

git reset HEAD file_1_path file_2_path file_3_path

Easy fix, btw this will not change / delete the new files.

Solution 23 - Git

I had the same problem however none of the above solutions worked for me. What I ended up doing was:

  • create an empty file with the same name
  • compare this file with its local history
  • copy history across to empty file.

Solution 24 - Git

Situation: One deleted a file but didn’t commit

If one deleted a file, and immediately realized it was a mistake? This one is easy, just do:

git checkout HEAD <filename>

If it is a folder, just do:

git checkout HEAD <foldername>/

Reference: https://www.git-tower.com/learn/git/faq/restoring-deleted-files

Solution 25 - Git

I had the same problem and none of the answers here I tried worked for me either. I am using Intellij and I had checked out a new branch git checkout -b minimalExample to create a "minimal example" on the new branch of some issue by deleting a bunch of files and modifying a bunch of others in the project. Unfortunately, even though I didn't commit any of the changes on the new "minimal example" branch, when I checked out my "original" branch again all of the changes and deletions from the "minimal example" branch had happened in the "original" branch too (or so it appeared). According to git status the deleted files were just gone from both branches.

Fortunately, even though Intellij had warned me "deleting these files may not be fully recoverable", I was able to restore them (on the minimal example branch from which they had actually been deleted) by right-clicking on the project and selecting Local History > Show History (and then Restore on the most recent history item I wanted). After Intellij restored the files in the "minimal example" branch, I pushed the branch to origin. Then I switched back to my "original" local branch and ran git pull origin minimalExample to get them back in the "original" branch too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHomanView Question on Stackoverflow
Solution 1 - GitNoufal IbrahimView Answer on Stackoverflow
Solution 2 - GitCB BaileyView Answer on Stackoverflow
Solution 3 - GitseddonymView Answer on Stackoverflow
Solution 4 - GitPaoloCView Answer on Stackoverflow
Solution 5 - GitPaul TView Answer on Stackoverflow
Solution 6 - GitAurangzebView Answer on Stackoverflow
Solution 7 - GitChris HinshawView Answer on Stackoverflow
Solution 8 - GitMuhammad SolimanView Answer on Stackoverflow
Solution 9 - GitCasper WilkesView Answer on Stackoverflow
Solution 10 - GitDustin GetzView Answer on Stackoverflow
Solution 11 - GitrickfoosusaView Answer on Stackoverflow
Solution 12 - GitPaulo Linhares - PackappsView Answer on Stackoverflow
Solution 13 - GitRickView Answer on Stackoverflow
Solution 14 - Gituser2453404View Answer on Stackoverflow
Solution 15 - GitstevecView Answer on Stackoverflow
Solution 16 - GitPPBView Answer on Stackoverflow
Solution 17 - GitLiyong ZhouView Answer on Stackoverflow
Solution 18 - GitDinis CruzView Answer on Stackoverflow
Solution 19 - GitAshutosh ShuklaView Answer on Stackoverflow
Solution 20 - GitVasyl ShyrochukView Answer on Stackoverflow
Solution 21 - GitHenrique FlorencioView Answer on Stackoverflow
Solution 22 - GitKaran SharmaView Answer on Stackoverflow
Solution 23 - GittheEUGView Answer on Stackoverflow
Solution 24 - GitFrancis BaconView Answer on Stackoverflow
Solution 25 - GitgeneSummonsView Answer on Stackoverflow