首页 > 解决方案 > 为什么 git add --update 将新文件添加到索引中?

问题描述

-u标志git add似乎并没有按照它记录的方式工作;有人可以解释我错过了什么吗?

-u--update)标志记录如下:

-u, --update
    Update the index just where it already has an entry matching <pathspec>. This removes as well as
    modifies index entries to match the working tree, but adds no new files.

    If no <pathspec> is given when -u option is used, all tracked files in the entire working tree are
    updated (old versions of Git used to limit the update to the current directory and its
    subdirectories).

根据我的阅读,普通git add -u文件不应将任何新文件添加到索引中,而应仅重新添加已暂存的任何文件。这是我想要的行为。

但是,如果我修改一个文件然后运行git add -u​​. 修改后的文件就像我没有使用过-u. 我不明白如何将这种行为与“不添加新文件”的文档声明相协调。

标签: git

解决方案


看来您可能不认为签出提交中的所有内容都存在索引条目;但他们确实如此。并非每个索引条目都由(例如)显示git status,就像不是列出工作树中的每个文件一样;status仅显示对索引或工作树的更改。但是“被追踪”和“在索引中”的意思是一样的。

所以,这就是全部-u- 它可以防止跟踪未跟踪的文件。如果您只想提交已经进行了一些更改的文件,AFAIK 您将需要一个自定义解决方案。


推荐阅读