如何在不暂存的情况下 git 添加新文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11087143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:05:06  来源:igfitidea点击:

How can you git add a new file without staging it?

gitstaginggit-index

提问by bitmask

To use git effectively (and as intended) I make small atomic commits, while I do have longer sessions where I do change not only one thing. Thus, I make heavy use of git add -p. This doesn't work for completely new files, though, because I tend to forget them later on.

为了有效地(并按预期)使用 git,我进行了小的原子提交,而我确实有更长的会话,在这些会话中我不仅要更改一件事。因此,我大量使用git add -p. 但是,这不适用于全新的文件,因为我以后往往会忘记它们。

What I want to do is, tell gitthat there isa new file, I want it to track, but not stage it:

我想要做的是,告诉git一个新的文件,我想它来跟踪,但不是阶段,它

Example: Running git statusproduces:

示例:运行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

If I have a file fooin the C section, and I say git add fooit will go to the A section. If I say git add -N fooit will go to both A and B. However, that would mean it would be included in the next commit, at least as the fact that there is a new file.

如果我foo在 C 部分有一个文件,我说它git add foo会转到 A 部分。如果我说它git add -N foo会同时发送到 A 和 B。但是,这意味着它将包含在下一次提交中,至少因为有一个新文件。

I want it to go in section B exclusively, such that I can later add it to A with git add -por git add foo(or whatever).

我希望它专门进入 B 部分,以便我以后可以使用git add -pgit add foo(或其他方式)将其添加到 A 中。

Edit

编辑

Regarding the add -Nsolution, this doesn't work because if I try to commit after having said add -Nand nothaving added it properly, git complains because it doesn't know how to handle empty files:

关于add -N解决方案,这并不工作,因为如果我尝试话说回来之后提交add -N已增加得当,git的抱怨,因为它不知道如何处理空文件:

foo: not added yet
error: Error building trees

回答by VonC

With Git 2.5, git add -N/--intent-to-addis actually the right solution.
The new file won't be part of the next commit.

使用 Git 2.5,git add -N/--intent-to-add实际上是正确的解决方案。
新文件不会成为下一次提交的一部分。

See commit d95d728by Nguy?n Thái Ng?c Duy (pclouds) (merged in d0c692263):

参见Nguy?n Thái Ng?c Duy ( ) 的commit d95d728pclouds(合并在d0c692263 中):

diff-lib.c: adjust position of i-t-a entries in diff

diff-lib.c: 在 diff 中调整 ita 条目的位置

Problem:

问题:

Entries added by "git add -N" are reminder for the user so that they don't forget to add them before committing. These entries appear in the index even though they are not real. Their presence in the index leads to a confusing "git status" like this:

git add -N”添加的条目是对用户的提醒,以便他们在提交之前不要忘记添加它们。这些条目出现在索引中,即使它们不是真实的。它们在索引中的存在会导致git status像这样令人困惑的“ ”:

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

Changes not staged for commit:
        modified:   foo

If you do a "git commit", "foo" will not be included even though "status" reports it as "to be committed".

如果您执行“ git commit”,foo即使“ status”将其报告为“ to be committed” ,也不会包含“ ”。

Solution:

解决方案:

This patch changes the output to become

此补丁将输出更改为

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

no changes added to commit

That means:

这意味着:

Treat such paths as "yet to be added to the index but Git already know about them"; "git diff HEAD" and "git diff --cached HEAD" should not talk about them, and "git diff" should show them as new. + files yet to be added to the index.

将此类路径视为“尚未添加到索引中,但 Git 已经知道它们”;“ git diff HEAD”和“ git diff --cached HEAD”不应该谈论它们,而“ git diff”应该显示它们是新的。+ 尚未添加到索引中的文件。

回答by asmeurer

Maybe you could try writing some pre-commit hook that alerts you if you have untracked files. This will require you to always keep your git directory clean to work, though (and obviously you'll need to keep a up-to-date .gitignore).

也许您可以尝试编写一些预提交钩子,如果您有未跟踪的文件,它会提醒您。这将要求您始终保持 git 目录干净才能工作(显然您需要保持最新的 .gitignore)。

Also try git add -iwhich is similar to git add -pbut also has an interface for adding new files.

还可以尝试git add -i类似于git add -p但也有用于添加新文件的界面。

回答by Daenyth

You could commit an empty file with that path before making your changes. If you've already written things there, move the file away, make a blank file, commit that, then add -p as normal and git commit --amendso you don't have an "Add blank file" commit.

在进行更改之前,您可以使用该路径提交一个空文件。如果你已经在那里写了东西,把文件移开,制作一个空白文件,提交,然后像往常一样添加 -p,git commit --amend这样你就没有“添加空白文件”提交。