git恢复删除的文件,删除后未提交任何内容

本文翻译自:git recover deleted file where no commit was made after the delete

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 status shows: 并且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 . 为什么不git checkout . reset the workspace to HEAD ? 将工作区重置为HEAD


#1楼

参考:https://stackoom.com/question/oAUA/git恢复删除的文件-删除后未提交任何内容


#2楼

The output tells you what you need to do. 输出告诉您您需要做什么。 git reset HEAD cc.properties etc. git reset HEAD cc.properties

This will unstage the rm operation. 这将取消rm操作的阶段。 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. 之后,再次运行git status将告诉您您需要执行git checkout -- cc.properties来取回文件。

扫描二维码关注公众号,回复: 10780385 查看本文章

Update: I have this in my config file 更新:我的配置文件中有这个

$ git config alias.unstage
reset HEAD

which I usually use to unstage stuff. 我通常用它来撤消工作。


#3楼

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. 仅从已进行删除的索引中检出。


#4楼

Since you're doing a git checkout . 由于您正在执行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 您可以使用git reset HEAD --hard来实现

Warning 警告

Doing this may remove all your latest modifications and unstage your modifications, eg, you can lose work. 这样做可能会删除所有最新修改,并取消修改,例如,您可能会丢失工作。 It may be what you want, but check out the docs to make sure. 可能是您想要的,但请查看文档以确保。


#5楼

只需执行git checkout path/to/file-I-want-to-bring-back.txt


#6楼

Here is the command that helped me on my mac. 这是在Mac上对我有帮助的命令。 I tried a few of the other solutions but they did not work for me. 我尝试了其他一些解决方案,但它们对我不起作用。

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
发布了0 篇原创文章 · 获赞 51 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105450894