如何从暂存区删除单个文件(撤消 git add)?
情况:我有一个 Git 存储库,其中文件已在索引中。我对几个文件进行了更改,打开 Git 并使用“git add”将这些文件添加到我的暂存区域。
问题:如何从暂存区域中删除其中一个文件,但不将其从索引中删除或撤消对文件本身的更改?
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 ."
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
如果我正确理解了这个问题,您只需“撤消”对该文件所做的 git add 操作即可。
如果需要从暂存区删除单个文件,请使用
git reset HEAD --
如果需要删除整个目录(文件夹) ) 从暂存区域,使用
git reset HEAD --
您的修改将被保留。当您运行 git status 时,该文件将再次显示为已修改但尚未暂存。
有关详细信息,请参阅
git reset
手册页。If I understand the question correctly, you simply want to "undo" the
git add
that was done for that file.If you need to remove a single file from the staging area, use
git reset HEAD -- <file>
If you need to remove a whole directory (folder) from the staging area, use
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.See the
git reset
man page for details.,
,
git reset
无论您以前是否有任何提交,都可以工作。
git reset <file>
Works whether or not you have any previous commits.
因此,对 Tim Henigan 的答案稍作调整:您需要在文件名之前使用 -- 。它看起来像这样:
So, a slight tweak to Tim Henigan's answer: you need to use -- before the file name. It would look like this:
对于较新版本的 Git,有 git restore --staged。
当我使用 Git 版本
2.26.2.windows.1
执行git status
时,还建议取消暂存:(这篇文章显示,在早期版本中,此时建议使用
git reset HEAD
)我强烈推荐这篇文章解释了
git revert
、git Restore
和git Reset
之间的区别以及还有git Restore
的附加参数。For newer versions of Git there is
git restore --staged <file>
.When I do a
git status
with Git version2.26.2.windows.1
it is also recommended for unstaging:(This post shows, that in earlier versions
git reset HEAD
was recommended at this point)I can highly recommend this post explaining the differences between
git revert
,git restore
andgit reset
and also additional parameters forgit restore
.如果您对 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.
我认为你可以选择
git Restore --staged
I think you could go for
git restore --staged <path/to/file_name>
如果您只想删除文件更改的子集,您可以使用:
或者
此命令基本上与 git add -p 相反:它只会从暂存中删除选定的更改区域。我发现它对于“取消添加”我错误添加的内容非常有用。
In case you just want to remove a subset of the changes to your file, you can use:
or
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.我认为您可能对索引的概念感到困惑,正如@CB Bailey评论的那样:
您可以简单地将暂存目录和索引视为同一事物。
所以,就像 @Tim Henigan 的回答一样,我猜:
这是我的答案:
通常,有两种方法可以撤消 stage 操作,正如其他答案已经提到的:
和
但有什么区别?
假设该文件已被暂存并且也存在于工作目录中,如果您想删除它,请使用
git rm --cached
来自暂存目录,并将文件保存在工作目录中。但请注意,如果您在这个操作,你会看到这个:
这是从暂存目录中删除文件的记录。如果您不想保留该记录而只是想撤消文件的前一阶段操作,请改用 git reset HEAD。
-------- 答案结束 --------
PS:我注意到提到了一些答案:
git checkout --
此命令适用于文件已被 删除的情况。 >已暂存,但文件暂存后在工作目录中已被修改,使用此操作可从暂存中恢复工作目录中的文件目录。换句话说,在此操作之后,更改发生在您的工作目录中,而不是您的暂存目录中。
I think you probably got confused with the concept of index, as @CB Bailey commented:
You can simply consider staging directory and index as the same thing.
So, just like @Tim Henigan's answer, I guess:
Here is my answer:
Commonly, there are two ways to undo a stage operation, as other answers already mentioned:
and
But what is the difference?
Assume the file has been staged and exists in working directory too, 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 directory but also mark the file asdeleted
in staging directory, if you useafter this operation, you will see this :
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.-------- END OF ANSWER --------
PS: I have noticed some answers mentioned:
git checkout -- <file>
This command is for the situation when the file has been staged, but the file has been modified in working directory after it was staged, use this operation to restore the file in working directory from staging directory. In other words, after this operation, changes happen in your working directory, NOT your staging directory.
在
2.23
版本之后,Git 引入了git Restore
命令,您可以使用它来执行此操作。引用官方文档:因此,您可以调用 git Restore --staged并取消暂存文件,但也保留所做的更改。请记住,如果文件未暂存,您将丢失对其所做的所有更改。
After version
2.23
, Git has introduced thegit restore
command which you can use to do that. Quoting the official documentation:So you can invoke
git restore --staged <path>
and unstage the file but also keep the changes you made. Remember that if the file was not staged you lose all the changes you made to it.如果您想删除遵循某种模式的文件并且您正在使用 git rm --cached,您也可以使用 file-glob 模式。
请参阅此处 。
If you want to remove files following a certain pattern and you are using
git rm --cached
, you can use file-glob patterns too.See here.
当您执行 git status 时,Git 会告诉您如何取消暂存:
因此 git reset HEAD对我有用,并且更改未受影响。
When you do
git status
, Git tells you how to unstage:So
git reset HEAD <file>
worked for me and the changes were un-touched.您想要:
影响单个文件
从暂存区域中删除文件
不从索引中删除单个文件
不要撤消更改本身
,解决方案是
或
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
or
您需要位于该文件的目录中,然后在终端中输入以下内容
You need to be in the directory of the file and then type the following into the terminal
如果您对许多跟踪文件进行了更改,但只想暂存其中一些文件,那么执行
git add .
并不总是有利的(或推荐的) - 因为它会暂存所有跟踪的文件(某些情况下)您只想将更改保留给您自己,并且不想将它们暂存到远程存储库)。
执行一堆
git add path/to/file1 path/to/file2
如果你有很多嵌套目录(大多数项目都是这种情况),那么
也不理想 - 会很烦人,这就是 Git 的时候GUI 很有帮助(可能只是我使用它的时候)。只需打开 Git GUI,它就会显示暂存和未暂存的文件部分。从暂存部分中选择要取消暂存的文件,然后按
取消暂存它们。
If you make changes to many tracked files but only want to stage a few of them, doing a
git add .
isn't always favorable (or recommended)- as it stages all the tracked files (some cases where you want to keep changes only to yourself and don't want to stage them to the remote repository).
nor is it ideal doing a bunch of
git add path/to/file1 path/to/file2
if you have a lot of nested directories (which is the case in most projects) - gets annoying
That's when Git GUI is helpful (probably only time I use it). Just open Git GUI, it shows staged and unstaged file sections. Select the files from the staged section that you want to unstage and press
to unstage them.
我的样本:
正如您可能注意到的
My sample:
As you may notice
您需要进入该文件的目录,然后在终端中键入以下内容
假设您只需要重置一个文件。
You need to be in the directory of the file and then type the following into the terminal
Assumption is that you need to reset one file only.
要立即取消暂存所有内容,请运行此命令
To unstage everything at once, run this command
git checkout --
它可以完美地从暂存区域中删除文件
git checkout -- <file>
It works perfectly to remove files from Staging Area