git - git add --intent-to-add 或 -N 的作用是什么,应该在什么时候使用?

标签 git

git add -h 上,我可以看到以下选项:

-N, --intent-to-add   record only the fact that the path will be added later

但是我不明白什么时候应该使用这个选项。该选项的实际作用是什么,应该如何使用?

最佳答案

启用未跟踪文件的比较

Blue112's answer 部分正确。 git add --intent-to-add 确实会为工作副本中每个指定的未跟踪文件添加一个空文件到暂存区/索引,但这样做的主要目的之一是使您能够使用 git diff 尚未添加到 Git 存储库中的文件 通过将未跟踪的文件与暂存中的空版本进行比较地区:

$ echo foo > foo.txt
$ git diff foo.txt

$ git add --intent-to-add foo.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

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

        new file:   foo.txt

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:   foo.txt

$ git diff --staged foo.txt
diff --git a/foo.txt b/foo.txt
new file mode 100644
index 0000000..e69de29

$ git diff foo.txt
diff --git a/foo.txt b/foo.txt
index e69de29..257cc56 100644
--- a/foo.txt
+++ b/foo.txt
@@ -0,0 +1 @@
+foo

一旦你对文件进行了 diff,你可以通过简单地执行一个普通的 git add 将非空版本添加到暂存区/索引:

$ git add foo.txt

启用未跟踪文件的git commit -a

同样,由于 --intent-to-add 通过将这些文件的空版本添加到暂存区/索引,使 Git “已知”未跟踪的文件,它还允许您使用 git commit --allgit commit -a 将这些文件与您已知的已修改文件一起提交,否则您将无法执行此操作。

official Linux Kernel documentation for git commit 中所述:

using the -a [or --all] switch with the commit command [will] automatically "add" changes from all known files (i.e. all files that are already listed in the index)...and then perform the actual commit

文档

来自 official Linux Kernel git add documentation :

-N
--intent-to-add

Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

关于git - git add --intent-to-add 或 -N 的作用是什么,应该在什么时候使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329051/

相关文章:

git - 我如何在 msysgit vim 中启用语法高亮和语法缩进?

git - 在 stash pop 期间发生 merge 冲突后的错误

Git:如何忽略对跟踪文件的更改?

git - 如何显示 `git log` 中的分支名称?

django - python 路径或项目中的多个 django-apps

linux - 使用公钥时 Git "ERROR:gitosis.serve.main:Repository read access denied"

windows - msys git 和长路径

git - 解决冲突 推送 再次重现相同的冲突

git - 拒绝git中的大文件

node.js - BitBucket 管道 - 使用 node/gulp/git ftp 构建和部署