如何从暂存区删除单个文件(撤消 git add)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1505948/
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
How do I remove a single file from the staging area (undo git add)?
提问by PHLAK
Situation:I have a Git repository with files already in the index. I make changes to several files, open Git and add these files to my staging area with "git add ."
情况:我有一个 Git 存储库,其中的文件已经在索引中。我对几个文件进行了更改,打开 Git 并使用“git add”将这些文件添加到我的暂存区。
Question:How do I remove one of those files from the staging area but not remove it from the index or undo the changes to the file itself?
问题:如何从暂存区中删除这些文件之一,但不从索引中删除它或撤消对文件本身的更改?
回答by Tim Henigan
If I understand the question correctly, you simply want to "undo" the git add
that was done for that file.
如果我正确理解了这个问题,您只需“撤消”git add
为该文件所做的操作。
If you need to remove a single filefrom the staging area, use
如果您需要从暂存区中删除单个文件,请使用
git reset HEAD -- <file>
git reset HEAD -- <file>
If you need to remove a whole directory (folder)from the staging area, use
如果您需要从暂存区删除整个目录(文件夹),请使用
git reset HEAD -- <directoryName>
git reset HEAD -- <directoryName>
Your modifications will be kept. When you run git status
the file will once again show up as modified but not yet staged.
您的修改将被保留。当您运行时,git status
该文件将再次显示为已修改但尚未暂存。
See the git reset
man pagefor details.
有关详细信息,请参阅git reset
手册页。
回答by user335425
git rm --cached FILE
,
,
git rm -r --cached CVS */CVS
回答by MTS
git reset <file>
git reset <file>
Works whether or not you have any previous commits.
无论您以前是否有任何提交,都可以使用。
回答by GuerillaNerd
So, a slight tweak to Tim Henigan's answer: you need to use -- before the file name. It would look like this:
因此,对 Tim Henigan 的回答稍作调整:您需要在文件名之前使用 -- 。它看起来像这样:
git reset HEAD -- <file>
回答by Vlad Cioaba
git reset filename.txt
If you have a modification in filename.txt , you added it to stage by mistake and you want to remove the file from staging but you don't want to lose the changes.
如果您对 filename.txt 进行了修改,则是您错误地将其添加到 stage 中,并且您想从 staging 中删除该文件,但又不想丢失更改。
回答by fede1024
In case you just want to remove a subset of the changes to your file, you can use:
如果您只想删除对文件所做更改的子集,您可以使用:
git reset -p
or
或者
git reset -p <file_name>
This command is basically the reverse of git add -p
: it will only remove the selected changes from the staging area. I find it extremely useful in "unadding" something that I added by mistake.
此命令基本上与 相反git add -p
:它只会从暂存区中删除选定的更改。我发现它在“取消添加”我错误添加的内容时非常有用。
回答by thilinarmtb
回答by Do Nhu Vy
You want:
你要:
Affect to a single file
Remove file from staging area
Not remove single file from index
Don't undo the change itself
影响单个文件
从暂存区删除文件
不从索引中删除单个文件
不要撤消更改本身
and the solution is
解决方案是
git reset HEAD file_name.ext
or
或者
git reset HEAD path/to/file/file_name.ext
回答by Do Nhu Vy
When you do git status
, Git tells you how to unstage:
当你这样做时git status
,Git 会告诉你如何取消暂存:
Changes to be committed: (use "git reset HEAD <file>..." to unstage).
So git reset HEAD <file>
worked for me and the changes were un-touched.
所以git reset HEAD <file>
对我有用,变化没有受到影响。
回答by Wulfric Lee
I think you probably got confused with the concept of index, as @CB Bailey commented:
我认为您可能对index的概念感到困惑,正如@CB Bailey 评论的那样:
The staging area is the index.
暂存区是索引。
You can simply consider staging directoryand indexas the same thing.
So, just like @Tim Henigan's answer, I guess:
您可以简单地将暂存目录和索引视为同一件事。
所以,就像@Tim Henigan 的回答一样,我猜:
you simply want to "undo" the
git add
that was done for that file.
您只想“撤消”
git add
为该文件完成的操作。
Here is my answer:
这是我的回答:
Commonly, there are two ways to undo a stageoperation, as other answers already mentioned:
通常,有两种方法可以撤消阶段操作,正如其他答案已经提到的:
git reset HEAD <file>
and
和
git rm --cached <file>
But what is the difference?
但有什么区别呢?
Assume the file has been stagedand exists in working directorytoo, use git rm --cached <file>
if you want to remove it from staging directory, and keep the file in working directory. But notice that this operation will not only remove the file from staging directorybut also mark the file as deleted
in staging directory, if you use
假设文件已经暂存并且也存在于工作目录中,git rm --cached <file>
如果要将其从暂存目录中删除,请使用,并将文件保留在工作目录中。但请注意,此操作不仅会从暂存目录中删除文件,还会将文件标记为deleted
在暂存目录中,如果您使用
git status
after this operation, you will see this :
此操作后,您将看到:
deleted: <file>
It's a record of removing the file from staging directory. If you don't want to keep that record and just simply want to undo a previous stage operation of a file, use git reset HEAD <file>
instead.
这是从暂存目录中删除文件的记录。如果您不想保留该记录而只想撤消文件的前一阶段操作,请git reset HEAD <file>
改用。
-------- END OF ANSWER --------
-------- 答案结束 --------
PS: I have noticed some answers mentioned:
PS:我注意到提到的一些答案:
git checkout -- <file>
git checkout -- <file>
This command is for the situation when the file has been staged, but the file has been modified in working directoryafter it was staged, use this operation to restore the file in working directoryfrom staging directory. In other words, after this operation, changes happen in your working directory, NOT your staging directory.
该命令适用于文件已被暂存,但文件被暂存后在工作目录中被修改的情况,使用该操作将工作目录中的文件从暂存目录中恢复。换句话说,在此操作之后,更改会发生在您的工作目录中,而不是您的暂存目录中。