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

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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 ..." to unstage) # #   deleted:    cc.properties #   deleted:    store/README #   deleted:    store/cc.properties # 

Why doesn't git checkout . reset the workspace to HEAD?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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

git ls-files -d | xargs 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 -- 


回答5:

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.



回答6:

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 


回答7:

if you u used

git rm filename 

to delete a file then

git checkout path/to/filename 

doesn't work, so in that case

git checkout HEAD^ filename 

should work



回答8:

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


回答9:

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


回答10:

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  


回答11:

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

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

$ git checkout -- file.cpp 


回答12:

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


回答13:

if you are looking for a deleted directory.

 git checkout ./pathToDir/* 


回答14:

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



回答15:

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)



回答16:

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.



回答17:

I also deleted all my files by accident and restored using the following command:

$ git config alias.unstage $ git checkout HEAD^ * 


回答18:

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 

Now you local branch would have all files in particular



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!