在 git 中取消暂存已删除的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9591407/
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
Unstage a deleted file in git
提问by lurscher
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 addinga file? I would like to know how to unstage that change as well.
加分点:另外,如果我想放弃的更改是添加文件怎么办?我也想知道如何取消这种变化。
回答by twalberg
Assuming you're wanting to undo the effects of git rm <file>
or rm <file>
followed by git add -A
or something similar:
假设您要撤消git rm <file>
或rm <file>
后跟git add -A
或类似的效果:
# 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 <file>
, the first line above suffices, assuming you haven't committed yet.
要撤消git add <file>
,上面的第一行就足够了,假设您还没有提交。
回答by seppo0010
Both questions are answered in git status
.
这两个问题都在git status
.
To unstage adding a new file use git rm --cached filename.ext
要取消暂存添加新文件,请使用 git rm --cached filename.ext
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test
To unstage deleting a file use git reset HEAD filename.ext
取消暂存删除文件使用 git reset HEAD filename.ext
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: test
In the other hand, git checkout --
never unstage, it just discards non-staged changes.
另一方面,git checkout --
永远不要取消暂存,它只会丢弃未暂存的更改。
回答by Ben Hymanson
The answers to your two questions are related. I'll start with the second:
你的两个问题的答案是相关的。我将从第二个开始:
Once you have staged a file (often with git add
, though some other commands implicitly stage the changes as well, like git rm
) you can back out that change with git reset -- <file>
.
一旦您暂存文件(通常使用git add
,尽管其他一些命令也隐式暂存更改,例如git rm
),您可以使用git reset -- <file>
.
In your case you must have used git rm
to remove the file, which is equivalent to simply removing it with rm
and then staging that change. If you first unstage it with git reset -- <file>
you can then recover it with git checkout -- <file>
.
在您的情况下,您必须使用git rm
删除文件,这相当于简单地删除它,rm
然后暂存该更改。如果您首先使用它取消暂存,git reset -- <file>
则可以使用git checkout -- <file>
.
回答by michaeldever
If it has been staged and committed, then the following will reset the file:
如果它已被暂存并提交,则以下内容将重置文件:
git reset COMMIT_HASH file_path
git checkout COMMIT_HASH file_path
git add file_path
This will work for a deletion that occurred several commits previous.
这将适用于之前发生多次提交的删除。
回答by Kreempuff
As of git v2.23, you have another option:
从git v2.23 开始,您还有另一个选择:
git restore --staged -- <file>
git restore --staged -- <file>