如何在 git 中重新添加添加的文件(仅更新暂存文件)?

How to re-add added files (update staged files only) in git?

我们修改了 path/to/another/path/to/main/ 中的文件。
path/to/main/ 中的文件已添加到 git 缓存中,但我们已更新 path/to/main/Bar.php 文件 AGAIN。我们现在有以下情况:

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

        modified:   path/to/main/Foo.php
        modified:   path/to/main/Bar.php

Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   path/to/main/Bar.php
        modified:   path/to/another/Aaa.php
        modified:   path/to/another/Bbb.php

(注意 path/to/main/Bar.php 出现了两次)

我需要一个命令,它可以读取之前添加的文件,而无需使用特定路径。

P.S。 git add --update 将添加所有这些文件。没用。

P.P.S。此命令应该能够读取 modified:new file: 类型。

UPD1

感谢@AyonNahiyan,是的,它可以在 bash 中工作。但是也许有一个命令没有使用 bash 技巧(子命令)。

这显示了仅暂存的文件列表:

git diff --name-only --cached

现在对这些文件进行新的更改(它将在 bash 中工作)

git add $(git diff --name-only --cached)

PS:(前提条件)当 运行 这些命令时,您需要位于 git 存储库的根目录。 运行 在任何子文件夹中都不起作用

git update-index 可用于此目的。

特别是,

git update-index --again

应该可以。

--again 选项选择已经在索引中的与 HEAD 不同的文件。

update-index 的实际操作是将这些文件的新内容拉入索引。

git update-index --again

会做你想做的。请注意,此命令默认在当前工作目录(和子目录,递归)上运行。如果要将命令应用于整个 repo,请添加 pathspec:

git update-index --again :/:

:/:这里的意思是“工作树的根”。

P.S。您可以将此命令的别名添加到您的 .gitconfig 文件中,例如:

[alias]
    readd = update-index --again :/:

Ayon 的答案有效。 对已删除文件的可能修复是添加另一个参数。

--diff-filter=d

这排除了“git 添加”中的所有已删除文件。 有关如何使用此参数的更多信息,请参见此线程: Filter git diff by type of change