简体   繁体   English

你如何git添加一个新的文件而不进行分期?

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

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. 为了有效地使用git(并且按照预期),我进行了小型的原子提交,而我确实有更长的会话,我不仅要改变一件事。 Thus, I make heavy use of git add -p . 因此,我大量使用git add -p This doesn't work for completely new files, though, because I tend to forget them later on. 但是,这对于全新的文件不起作用,因为我以后往往会忘记它们。

What I want to do is, tell git that there is a new file, I want it to track, but not stage it : 我想要做的是,告诉git一个新的文件,我想它来跟踪,而不是阶段,它

Example: Running git status produces: 示例:运行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 foo in the C section, and I say git add foo it will go to the A section. 如果我在C部分有一个文件foo ,并且我说git add foo它将转到A部分。 If I say git add -N foo it 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. 如果我说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 -p or git add foo (or whatever). 我想让它专门进入B部分,这样我以后可以使用git add -pgit add foo (或其他)将其添加到A.

Edit 编辑

Regarding the add -N solution, this doesn't work because if I try to commit after having said add -N and not having 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

With Git 2.5, git add -N/--intent-to-add is actually the right solution. 使用Git 2.5, git add -N/--intent-to-add实际上是正确的解决方案。
The new file won't be part of the next commit. 新文件不会成为下一次提交的一部分。

See commit d95d728 by Nguyễn Thái Ngọc Duy ( pclouds ) (merged in d0c692263 ): 请参阅NguyễnTháiNgọcDuy 提交的d95d728pclouds )(合并于d0c692263 ):

diff-lib.c : adjust position of ita entries in diff diff-lib.c :调整diff中条目的位置

Problem: 问题:

Entries added by " git add -N " are reminder for the user so that they don't forget to add them before committing. 由“ git add -Ngit add -N条目是用户的提醒,以便他们在提交之前不会忘记添加它们。 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 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 ”,即使“ status ”将其报告为“ to be committed git commit ”,也不会包含“ foo ”。

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已经了解它们”; " git diff HEAD " and " git diff --cached HEAD " should not talk about them, and " git diff " should show them as new. git diff HEAD ”和“ git diff --cached HEAD ”不应该谈论它们,而“ git diff ”应该将它们显示为新的。 + files yet to be added to the index. +尚未添加到索引的文件。

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 -i which is similar to git add -p but also has an interface for adding new files. 还可以尝试git add -i ,它类似于git add -p但也有一个用于添加新文件的界面。

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 --amend so you don't have an "Add blank file" commit. 如果你已经在那里写了东西,移动文件,制作一个空白文件,提交,然后正常添加-p和git commit --amend这样你就没有“添加空白文件”提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM