git git恢复删除的文件,删除后没有提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11956710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:24:08  来源:igfitidea点击:

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

git

提问by Homan

I deleted some files.

我删除了一些文件。

I did NOT commit yet.

我还没有提交。

I want to reset my workspace to recover the files.

我想重置我的工作区以恢复文件。

I did a git checkout ..

我做了一个git checkout ..

But the deleted files are still missing.

但是删除的文件仍然丢失。

And git statusshows:

git status显示:

# 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 checkout .将工作区重置为HEAD

采纳答案by Noufal Ibrahim

The output tells you what you need to do. git reset HEAD cc.propertiesetc.

输出会告诉您需要做什么。git reset HEAD cc.properties等等。

This will unstage the rm operation. After that, running a git statusagain will tell you that you need to do a git checkout -- cc.propertiesto get the file back.

这将取消 rm 操作。之后,git status再次运行 a会告诉您需要执行 agit checkout -- cc.properties才能取回文件。

Update: I have this in my config file

更新:我的配置文件中有这个

$ git config alias.unstage
reset HEAD

which I usually use to unstage stuff.

我通常用它来取消暂存的东西。

回答by CB Bailey

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.

git checkout .仅从已执行删除操作的索引中检出。

回答by seddonym

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

做就是了 git checkout path/to/file-I-want-to-bring-back.txt

回答by PaoloC

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

要一次自动恢复所有未暂存的删除,无需指定每个路径:

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

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

要一次自动恢复所有分阶段删除,而不指定每个路径:

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

回答by Paul T

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

由于您正在执行 a git checkout .,看起来您正在尝试将您的分支恢复到上次提交状态。

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

你可以用一个 git reset HEAD --hard

Warning

警告

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

这样做可能会删除所有最新的修改并取消您的修改,例如,您可能会丢失工作。这可能是您想要的,但请查看文档以确保。

回答by Aurangzeb

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

应该管用

回答by Chris Hinshaw

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.

这是在我的 mac 上帮助我的命令。我尝试了其他一些解决方案,但它们对我不起作用。

Git version on OSX Mavericks

OSX Mavericks 上的 Git 版本

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

Command

命令

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

回答by Dustin Getz

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

回答by Casper Wilkes

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.

请记住使用句点,因为它告诉 git 获取所有文件。

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,你会得到:

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

回答by rickfoosusa

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

使用git ls-files结帐删除(-d)或修改(-m)文件。

git checkout $(git ls-files -d)

see How can I restore only the modified files on a git checkout?

请参阅如何在 git checkout 中仅恢复已修改的文件?