在git中卸载已删除的文件 [英] Unstage a deleted file in git

查看:181
本文介绍了在git中卸载已删除的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,放弃对文件的更改可以做到:

  git checkout  - < file> 

如果我想放弃的更改会删除文件,该怎么办?上面的行会给出错误:

 错误:pathspec'< file>'与任何已知的文件都不匹配去玩。 

什么命令会恢复单个文件而不撤消其他更改?

加分:另外,如果我想放弃的更改是添加文件,该怎么办?我想知道如何取消这种变化。 解决方案

假设你想撤销'git rm'或'rm'后跟'git add -A'或类似的名称:

 #这将恢复文件状态index 
git reset - < file>
#然后从索引
签出一份副本git checkout - < file>

要撤消'git add',假设你还没有提交,上面的第一行就足够了。

Usually, to discard changes to a file you would do:

git checkout -- <file>

What if the change I want to discard is deleting the file? The above line would give an error:

error: pathspec '<file>' did not match any file(s) known to git.

What command will restore that single file without undoing other changes?

bonus point: Also, what if the change I want to discard is adding a file? I would like to know how to unstage that change as well.

解决方案

Assuming you're wanting to undo the effects of 'git rm ' or 'rm ' followed by 'git add -A' or something similar:

# this restores the file status in the index
git reset -- <file>
# then check out a copy from the index
git checkout -- <file>

To undo 'git add ', the first line above suffices, assuming you haven't committed yet.

这篇关于在git中卸载已删除的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆