你怎么可以添加一个新的文件而不登台呢? [英] How can you git add a new file without staging it?

查看:129
本文介绍了你怎么可以添加一个新的文件而不登台呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了有效地使用git(并且按照预期),我做了小的原子提交,而我确实有更长的会话,我不仅仅改变一件事情。因此,我大量使用 git add -p 。这对于全新的文件并不适用,因为我以后会忘记它们。



我想要做的是告诉 git 那里一个新文件,我希望它能够跟踪,但是不会对它进行分级:

示例:运行 git status 产生:

 #在分支my-current-branch 
#你的分支在2次提交之前领先于origin / my-current-branch。

#要提交的更改:

<<< STAGED SECTION>> //

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

<<< UNSTAGED-YET-KNOWN SECTION>> // b

#未经记录的文件:
#(使用git add< file> ...来包含将提交的内容)

<<未知部分>> // C

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



我希望它在B节中独占,这样我可以稍后使用将它添加到A中。git add - 或 git add foo (或其他)。

编辑



关于添加-N 解决方案,这不起作用,因为如果我在说添加-N 不是添加它,git抱怨,因为它不知道如何处理空文件:

  foo:尚未添加
错误:构建树时出错


<使用Git 2.5, git add -N / - intent-to-add

实际上是正确的解决方案。

新文件不会成为下一次提交的一部分。



请参阅 pclouds )提供的提交d95d728 (合并在 d0c692263 ):


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



问题:




通过 git add -N 添加的条目是提醒用户,以便他们在提交之前不会忘记添加它们。这些条目即使不是真实的,也会出现在索引中。他们在索引中的存在导致了一个令人困惑的 git status ,如下所示:



 关于分支大师
要提交的更改:
新文件:foo

未针对commit提交的更改:
已修改:foo




如果您执行 git即使
status ,也不会包含提交
foo >将其报告为要提交




解决方案:
$ b


输出成为



 在分支主设备上
更改没有为commit提交:
新文件:foo

没有更改添加到提交

意思是:


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



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.

What I want to do is, tell git that there is a new file, I want it to track, but not stage it:

Example: Running git status produces:

# 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. 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.

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).

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:

foo: not added yet
error: Error building trees

解决方案

With Git 2.5, git add -N/--intent-to-add is actually the right solution.
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):

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

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:

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".

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.

这篇关于你怎么可以添加一个新的文件而不登台呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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