在Git中开始跟踪一个文件,而不将其添加到索引中。

17
可以在git中开始跟踪文件而不将它们添加到索引中吗?
我有一些新文件,我希望它们在git clean之后仍然存在,但很可能在下次提交之前发生变化。我现在只需将它们添加到索引中,然后在提交之前再次添加它们吗?
3个回答

23

仅作简短备注:虽然这个解决方案在许多情况下可能有效,但不要用它来将新文件包含在存储区(我刚刚尝试过,目前不起作用)。另请参阅:https://dev59.com/dsD5oIgBc1ULPQZFOmFa - kaba
要将新文件添加到暂存区,最好使用git stash save -u命令,还可以参考https://dev59.com/1HRA5IYBdhLWcg3wxA1N。 - Wiktor Czajkowski

4
您可以使用git add来将文件存储在暂存区中,然后在提交之前使用git reset来将它们撤回。

0

add -N 的问题(即“意图添加”或“i-t-a”,是为了在 Git 中开始跟踪一个文件但不将其添加到索引中)是:

  • 由于“意图添加”条目的实现细节,当前的“git diff”命令(即没有 treeish 或 --cached 参数)将显示 i-t-a 文件中的更改,但它不会将该文件标记为新文件,
  • 而“diff --cached”会将文件标记为新文件,同时显示其内容为空。

Git 2.19(2018 年第三季度)对此进行了更改;因为“git diff”命令比较索引和工作树。
对于使用“意图添加”位添加的路径,该命令会显示它们的完整内容已添加,但这些路径本身未被标记为新文件。
它们现在默认显示为新文件。

请查看 commit cff5dc0, commit 8fc8f05, commit 0231ae7, commit ba4e356 (2018年5月26日) 由 Nguyễn Thái Ngọc Duy (pclouds) 提交。
(由 Junio C Hamano -- gitster -- 合并于 commit ac997db, 2018年6月25日)

在 Git 2.19 之前:

$ git diff                      | $ diff --cached
--------------------------------|-------------------------------
 diff --git a/new b/new         | diff --git a/new b/new
 index e69de29..5ad28e2 100644  | new file mode 100644
 --- a/new                      | index 0000000..e69de29
 +++ b/new                      |
 @@ -0,0 +1 @@                  |
 +haha                          |

One evidence of the current output being wrong is that, the output from "git diff" (with ita entries) cannot be applied because it assumes empty files exist before applying.

Turning on --ita-invisible-in-index (commit 425a28e, commit b42b451 Oct. 2016, Git 2.11.0) would fix this. The result is "new file" line moving from "git diff --cached" to "git diff".

$ git diff                      | $ diff --cached
--------------------------------|-------------------------------
 diff --git a/new b/new         |
 new file mode 100644           |
 index 0000000..5ad28e2         |
 --- /dev/null                  |
 +++ b/new                      |
 @@ -0,0 +1 @@                  |
 +haha                          |

This option is on by default in git-status but we need more fixup in rename detection code (commit bc3dca0, Jan. 2018, Git 2.17.0). Luckily we don't need to do anything else for the rename detection code in diff.c (wt-status.c uses a customized one).


在 Git 2.28(2020 年第三季度)中,“git diff-files”已经学会了说标记为意图添加的路径是新文件,而不是从空 blob 修改而来。

请参见 commit feea694(2020 年 6 月 20 日),作者是 Srinidhi Kaushik (clickyotomy)
(由 Junio C Hamano -- gitster --commit 298d704 中合并,于 2020 年 6 月 29 日)

diff-files:将“i-t-a”文件视为“not-in-index

签名:Srinidhi Kaushik

diff-files命令及相关调用函数cmd_diff_files()的命令,在比较工作树和索引时,将"意图添加"文件视为索引的一部分。
这在之前的提交中已经得到解决,包括0231ae71d3diff:默认打开--ita-invisible-in-index选项,2018-05-26)和425a28e0a4diff-lib:允许将ita条目视为“尚未存在于索引中”,2016-10-24),通过默认打开选项--ita-invisible-in-index(在b42b451919("diff:添加--ita-[in]visible-in-index",2016-10-24,Git v2.11.0-rc0 -- merge)中引入)来实现。

对于diff-files(以及随之而来的add -p),为了将i-t-a文件显示为新文件,这里也将默认启用ita_invisible_in_index选项。


警告:如果您在 git diff-filter 中同时使用排除(小写)和包含(大写),请使用 Git 2.36 (Q2 2022)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接