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来取回文件。

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值