git add --intent-to-add或-N做什么,它应该在什么时候使用? [英] What does git add --intent-to-add or -N do and when should it be used?

查看:183
本文介绍了git add --intent-to-add或-N做什么,它应该在什么时候使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  -N,--intent-to-add record只记录路径将被添加的事实



<但我不明白什么时候应该使用这个选项。这个选项实际上做了什么以及它应该如何使用? 解决方案

启用未跟踪文件的区分



Blue112的答案 部分正确。 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
分支主
您的分支与origin / master保持同步。

要提交的更改:
(使用git reset HEAD< file> ...来取消存储)

新文件:foo.txt

没有为commit提交更改:
(使用git add< file> ...更新将提交的内容)
(使用git checkout - < file> ; ...放弃工作目录中的更改)

修改:foo.txt

$ git diff --staged foo.txt
diff --git a / foo.txt b / foo.txt
新文件模式100644
索引0000000..e69de29

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

一旦您对文件进行了区分,您可以通过简单地执行正常的 git add 来将非空版本添加到暂存区域/索引中:

  $ git add foo.txt 



启用 git commit -a 未跟踪文件



Likewis e,因为 - intent-to-add 通过将这些文件的空白版本添加到暂存区域/索引中,使未跟踪文件知道Git,它还允许您使用 git commit --all git commit -a 来提交这些文件以及您已知的修改文件,即这是你无法做到的其他事情。



正如 git commit 的官方Linux内核文档:


使用 -a [或 - all ]使用 commit 命令切换[将]自动添加来自所有已知文件(即,所有已经列在索引中的文件)...然后执行实际提交



文档



官方Linux内核 git add 文档


  -N 
--intent-to-add

仅记录事实该路径将在稍后添加。路径条目放置在没有内容的索引中。除此之外,这对于使用 git diff 显示这些文件的非分离内容并将它们提交给 git commit -a



On git add -h I can see the following option:

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

But I don't understand when should this option be used. What does this option really do, and how it should be used?

解决方案

Enable diffing of untracked files

Blue112's answer is partially correct. git add --intent-to-add does indeed add an empty file to the staging area/index for each specified untracked file in your working copy, but one of the main purposes of this is to enable you to use git diff with files that haven't been added to the Git repository yet by diffing your untracked file against its empty version in the staging area:

$ 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

Once you've diffed the file, you can add the non-empty version to the staging area/index by simply doing a normal git add:

$ git add foo.txt

Enable git commit -a of untracked files

Likewise, since --intent-to-add makes untracked files "known" to Git by adding empty versions of those files to the staging area/index, it also allows you to use git commit --all or git commit -a to commit those files along with your known modified files, which is something that you wouldn't be able to do otherwise.

As explained in the 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

Documentation

From the 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 add --intent-to-add或-N做什么,它应该在什么时候使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆