为了有效地(并按预期使用)git,我进行了一些小的原子提交,而我确实有更长的 session ,在其中我不仅要更改一件事。因此,我大量使用了git add -p。但是,这对于全新文件不起作用,因为稍后我会忘记它们。
我想做的是,告诉git有一个新文件,我希望它跟踪而不是暂存它:
示例:运行git status会产生:

# On branch my-current-branch
# Your branch is ahead of 'origin/my-current-branch' by 2 commits.
#
# Changes to be committed:
#
<<STAGED SECTION>> // A
#
# 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)
#
<<UNSTAGED-YET-KNOWN SECTION>> // B
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
<<UNKNOWN SECTION>> // C
如果我在C部分中有一个文件foo,并且我说git add foo,它将进入A部分。如果我说git add -N foo,它将同时进入A和B。但是,这意味着它将被包含在下一次提交中,至少是因为存在一个新文件。
我希望它专门在B节中使用,以便以后可以使用git add -pgit add foo(或其他任何方式)将其添加到A中。
编辑
关于add -N解决方案,这是行不通的,因为如果我在说了add -N并且未正确添加后尝试提交,则git会抱怨,因为它不知道如何处理空文件:
foo: not added yet
error: Error building trees

最佳答案

在Git 2.5中, git add -N/--intent-to-add 实际上是正确的解决方案。
新文件将不属于下一次提交。

请参阅NguyễnTháiNgọcDuy(commit d95d728)(合并为 pclouds )的d0c692263:



问题:


On branch master
Changes to be committed:
        new file:   foo

Changes not staged for commit:
        modified:   foo



解:


On branch master
Changes not staged for commit:
        new file:   foo

no changes added to commit

这意味着:

10-01 02:45