如何从暂存区删除单个文件(撤消 git add)?

发布于 2024-08-06 07:29:42 字数 165 浏览 4 评论 0原文

情况:我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

策马西风 2024-08-13 07:29:43

如果我正确理解了这个问题,您只需“撤消”对该文件所做的 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.

顾忌 2024-08-13 07:29:43
git rm --cached FILE

,

git rm -r --cached CVS */CVS
git rm --cached FILE

,

git rm -r --cached CVS */CVS
囍孤女 2024-08-13 07:29:43

git reset

无论您以前是否有任何提交,都可以工作。

git reset <file>

Works whether or not you have any previous commits.

ˇ宁静的妩媚 2024-08-13 07:29:43

因此,对 Tim Henigan 的答案稍作调整:您需要在文件名之前使用 -- 。它看起来像这样:

git reset HEAD -- <file>

So, a slight tweak to Tim Henigan's answer: you need to use -- before the file name. It would look like this:

git reset HEAD -- <file>
︶葆Ⅱㄣ 2024-08-13 07:29:43

对于较新版本的 Git,有 git restore --staged

当我使用 Git 版本 2.26.2.windows.1 执行 git status 时,还建议取消暂存:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

(这篇文章显示,在早期版本中,此时建议使用git reset HEAD

我强烈推荐这篇文章解释了 git revertgit Restoregit Reset 之间的区别以及还有 git Restore 的附加参数。

For newer versions of Git there is git restore --staged <file>.

When I do a git status with Git version 2.26.2.windows.1 it is also recommended for unstaging:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

(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 and git reset and also additional parameters for git restore.

风筝有风,海豚有海 2024-08-13 07:29:43
git reset filename.txt

如果您对 filename.txt 进行了修改,则您错误地将其添加到了暂存阶段,并且您希望从暂存中删除该文件,但又不想丢失更改。

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.

场罚期间 2024-08-13 07:29:43

我认为你可以选择

git Restore --staged

I think you could go for

git restore --staged <path/to/file_name>

静水深流 2024-08-13 07:29:43

如果您只想删除文件更改的子集,您可以使用:

git reset -p

或者

git reset -p <file_name>

此命令基本上与 git add -p 相反:它只会从暂存中删除选定的更改区域。我发现它对于“取消添加”我错误添加的内容非常有用。

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.

清引 2024-08-13 07:29:43

我认为您可能对索引的概念感到困惑,正如@CB Bailey评论的那样:

暂存区域是索引。

您可以简单地将暂存目录索引视为同一事物。
所以,就像 @Tim Henigan 的回答一样,我猜:

您只是想“撤消”对该文件所做的 git add 操作。

这是我的答案:

通常,有两种方法可以撤消 stage 操作,正如其他答案已经提到的:

git reset HEAD <file>

git rm --cached <file>

但有什么区别?

假设该文件已被暂存并且也存在于工作目录中,如果您想删除它,请使用git rm --cached来自暂存目录,并将文件保存在工作目录中。但请注意,如果您

git status

在这个操作,你会看到这个:

        deleted:    <file>

这是从暂存目录中删除文件的记录。如果您不想保留该记录而只是想撤消文件的前一阶段操作,请改用 git reset HEAD

-------- 答案结束 --------

PS:我注意到提到了一些答案:

git checkout --

此命令适用于文件已被 删除的情况。 >已暂存,但文件暂存后在工作目录中已被修改,使用此操作可从暂存中恢复工作目录中的文件目录。换句话说,在此操作之后,更改发生在您的工作目录中,而不是您的暂存目录中。

I think you probably got confused with the concept of index, as @CB Bailey commented:

The staging area is the index.

You can simply consider staging directory and index as the same thing.
So, just like @Tim Henigan's answer, I guess:

you simply want to "undo" the git add that was done for that file.

Here is my answer:

Commonly, there are two ways to undo a stage operation, as other answers already mentioned:

git reset HEAD <file>

and

git rm --cached <file>

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 as deleted in staging directory, if you use

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.

-------- 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.

撩心不撩汉 2024-08-13 07:29:43

2.23 版本之后,Git 引入了 git Restore 命令,您可以使用它来执行此操作。引用官方文档:

使用某个目录中的某些内容恢复工作树中的指定路径
恢复源。如果路径被跟踪但在还原中不存在
源,它将被删除以匹配源。

该命令还可以用于恢复索引中的内容
--staged,或使用 --staged --worktree 恢复工作树和索引。

因此,您可以调用 git Restore --staged并取消暂存文件,但也保留所做的更改。请记住,如果文件未暂存,您将丢失对其所做的所有更改。

After version 2.23, Git has introduced the git restore command which you can use to do that. Quoting the official documentation:

Restore specified paths in the working tree with some contents from a
restore source. If a path is tracked but does not exist in the restore
source, it will be removed to match the source.

The command can also be used to restore the content in the index with
--staged, or restore both the working tree and the index with --staged --worktree.

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.

絕版丫頭 2024-08-13 07:29:43

如果您想删除遵循某种模式的文件并且您正在使用 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.

夜声 2024-08-13 07:29:43

当您执行 git status 时,Git 会告诉您如何取消暂存:

Changes to be committed: (use "git reset HEAD <file>..." to unstage).

因此 git reset HEAD对我有用,并且更改未受影响。

When you do git status, Git tells you how to unstage:

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.

书间行客 2024-08-13 07:29:43

您想要:

  • 影响单个文件

  • 从暂存区域中删除文件

  • 不从索引中删除单个文件

  • 不要撤消更改本身

,解决方案是

git reset HEAD file_name.ext

git reset HEAD path/to/file/file_name.ext

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
故事和酒 2024-08-13 07:29:43

您需要位于该文件的目录中,然后在终端中输入以下内容

git checkout -- <file>

You need to be in the directory of the file and then type the following into the terminal

git checkout -- <file>
爱的那么颓废 2024-08-13 07:29:43

如果您对许多跟踪文件进行了更改,但只想暂存其中一些文件,那么执行

git add .

并不总是有利的(或推荐的) - 因为它会暂存所有跟踪的文件(某些情况下)您只想将更改保留给您自己,并且不想将它们暂存到远程存储库)。

执行一堆

git add path/to/file1 path/to/file2

如果你有很多嵌套目录(大多数项目都是这种情况),那么

也不理想 - 会很烦人,这就是 Git 的时候GUI 很有帮助(可能只是我使用它的时候)。只需打开 Git GUI,它就会显示暂存和未暂存的文件部分。从暂存部分中选择要取消暂存的文件,然后按

Ctrl+U(对于 Windows)

取消暂存它们。

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

Ctrl+U (for windows)

to unstage them.

恋竹姑娘 2024-08-13 07:29:43

我的样本:

$ git status
On branch feature/wildfire/VNL-425-update-wrong-translation
Your branch and 'origin/feature/wildfire/VNL-425-update-wrong-translation' have diverged,
and have 4 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   ShopBack/Source/Date+Extension.swift
    modified:   ShopBack/Source/InboxData.swift
    modified:   ShopBack/en.lproj/Localizable.strings

正如您可能注意到的

> Changes to be committed:
>       (use "git reset HEAD <file>..." to unstage)

My sample:

$ git status
On branch feature/wildfire/VNL-425-update-wrong-translation
Your branch and 'origin/feature/wildfire/VNL-425-update-wrong-translation' have diverged,
and have 4 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   ShopBack/Source/Date+Extension.swift
    modified:   ShopBack/Source/InboxData.swift
    modified:   ShopBack/en.lproj/Localizable.strings

As you may notice

> Changes to be committed:
>       (use "git reset HEAD <file>..." to unstage)
一张白纸 2024-08-13 07:29:43

您需要进入该文件的目录,然后在终端中键入以下内容

git reset HEAD .

假设您只需要重置一个文件。

You need to be in the directory of the file and then type the following into the terminal

git reset HEAD .

Assumption is that you need to reset one file only.

何其悲哀 2024-08-13 07:29:43

要立即取消暂存所有内容,请运行此命令

git reset HEAD -- .

To unstage everything at once, run this command

git reset HEAD -- .
兰花执着 2024-08-13 07:29:43

git checkout --

它可以完美地从暂存区域中删除文件

git checkout -- <file>

It works perfectly to remove files from Staging Area

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文