在使用git add-N之后,文件不会进入提交 [英] File doesn′t get into the commit after using git add -N

查看:715
本文介绍了在使用git add-N之后,文件不会进入提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提交一个Python模块的 __ init __。py 文件,该文件在我的磁盘上已经包含代码。但是,对于当前的提交,我想将其添加为空,因为这部分代码尚未进行测试。因此,我用

  $ git add -N __init__.py 

该文件位于 git status 的输出中,但如果我发出 git commit ,除 __ init __。py 之外的所有其他文件进入提交,而依次保留在索引中根据 git status



git-add 的手册页对< c $ c> -N 即

 只记录路径将在稍后添加的事实。路径条目放置在没有内容的索引中。 

是否有办法规避稍后会被添加部分,即添加文件为空而不暂时删除它的内容?

编辑:这发生在当前(2.2.0)Git 。使用1.7.1和一个小测试回购,我得到一个错误:

  $ git status 
#在分支主人
#要提交的更改:
#(使用git reset HEAD< file> ...停用)

#新文件:b

#已更改但未更新:
#(使用git add< file> ...更新将提交的内容)
#(使用git checkout - <文件> ...放弃工作目录中的更改)

#修改:b

$ git commit -m'test'
b:not added但
错误:错误构建树


解决方案

应该使用Git 2.5(2015年第2季度)修复,这意味着 git commit 不会尝试包含稍后要添加的新文件。

请参阅NguyễnTháiNgọcDuy的 commit d95d728 >)。 nofollow noreferrer> d0c692263



diff-lib.c :在差异



中调整 ita 条目的位置

问题:




通过 git add -N 提醒用户,以便他们在提交之前不会忘记添加它们。即使它们不是真实的,这些条目也会出现在索引中。

它们在索引中的存在会导致令人困惑的 git status ,如下所示:



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

没有为commit提交的更改:
modified:foo




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




解决方案:
$ b


输出成为



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

没有更改添加到提交

在换句话说:



  • 将这些路径视为尚未添加到索引中,但Git已经知道它们;

  • git diff HEAD git diff --cached HEAD 不应该谈论它们,并且
  • git diff 应该将它们显示为尚未添加到索引的新文件。







2016年第四季度更新:


$ b


提交:修改空提交创建,当没有更改但ita条目


ita ita 是intent添加)



请参阅 commit 2c49f7f 提交018ec3c commit b42b451 承诺425a28e (2016年10月24日)作者:NguyễnTháiNgọcDuy( pclouds
(由 Junio C Hamano - gitster - commit 6503602 ,2016年10月27日)


git add -N ,足以绕过 git commit >的检查来避免从没有 - allow-empty 的空提交进行操作。

同样的逻辑阻止了 git status 更改没有为commit提交中显示新文件这样的路径。部分。


git diff 手册页现在包括:

   -  ita -invisible-in-index:




默认情况下,由 git add -N 添加的条目显示为 git diff ,并在 git diff --cached >中添加一个新文件。

这个选项使得条目在 git diff 中显示为新文件,并且在 git diff --cached >中不存在。


这个选项可以使用 - ita-visible-in-index 进行恢复。



这两个选项都是实验性的,可以在将来删除。






更新Q1 2018(Git 2.16.x / 2.17), git status 再次改善。

git status ,在移动工作树中的路径后(因此使得
它看起来被移除),然后用 -N 选项(因此
使得出现增加)检测到它是重命名的,但没有
正确报告新旧路径。



请参阅 commit 176ea74 commit 5134ccd 提交ea56f97 commit 98bc94e commit 06dba2b 提交6de5aaf (2017年12月27日)作者:NguyễnTháiNgọcDuy( pclouds

帮助:< a href =https://github.com/boogisha =nofollow noreferrer> Igor Djordjevic( boogisha )。

(由 Junio C Hamano - gitster 合并 - 提交bc3dca0 ,2018年1月23日)注意: ita ita 是打算添加 。


wt-stat us.c :handle worktree renames



之前 425a28e (diff-lib:允许ita条目被视为尚不存在
的索引 - 2016-10-24,Git 2.11.0-rc0)从来没有在索引中的新文件,从本质上禁用重命名检测,因为我们只检测到一个新文件出现在差异对时重命名。



在提交后,条目可以作为新文件出现在 git diff-files 中。
但是 wt-status中的diff回调函数.c 不处理这种情况并产生不正确的状态输出。



I want to commit a Python module’s __init__.py file, which, on my disk, already contains code. However, for the current commit, I wanted to add it empty, as this part of the code is yet to be tested. Thus, I used

$ git add -N __init__.py

The file is there in the output of git status, but if I issue git commit, all other files gets into the commit except __init__.py, which, in turn, remains in the index according to git status.

The man page of git-add says for -N that

Record only the fact that the path will be added later. An entry for the path is placed in the index with no content.

Is there a way to circumvent this will be added later part, i.e. add the file empty without temporarily deleting its content?

Edit: This happens with current (2.2.0) Git. With 1.7.1 and a small test repo, I get an error:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   b
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   b
#
$ git commit -m 'test'
b: not added yet
error: Error building trees

解决方案

This should be fixed with Git 2.5 (Q2 2015), meaning git commit won't try to include the new file "intended to be added later".
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

In other words:

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


Update Q4 2016:

commit: fix empty commit creation when there's no changes but ita entries

(ita or i-t-a is "intent to add")

See commit 2c49f7f, commit 018ec3c, commit b42b451, commit 425a28e (24 Oct 2016) by Nguyễn Thái Ngọc Duy (pclouds).
(Merged by Junio C Hamano -- gitster -- in commit 6503602, 27 Oct 2016)

When new paths were added by "git add -N" to the index, it was enough to circumvent the check by "git commit" to refrain from making an empty commit without "--allow-empty".
The same logic prevented "git status" to show such a path as "new file" in the "Changes not staged for commit" section.

git diff man page now includes:

--ita-invisible-in-index:

By default entries added by "git add -N" appear as an existing empty file in "git diff" and a new file in "git diff --cached".
This option makes the entry appear as a new file in "git diff" and non-existent in "git diff --cached".

This option could be reverted with --ita-visible-in-index.

Both options are experimental and could be removed in future.


Update Q1 2018 (Git 2.16.x/2.17), git status improves again.
"git status", after moving a path in the working tree (hence making it appear "removed"), and then adding with the -N option (hence making that appear "added") detected it as a rename, but did not report the old and new pathnames correctly.

See commit 176ea74, commit 5134ccd, commit ea56f97, commit 98bc94e, commit 06dba2b, commit 6de5aaf (27 Dec 2017) by Nguyễn Thái Ngọc Duy (pclouds).
Helped-by: Igor Djordjevic (boogisha).
(Merged by Junio C Hamano -- gitster -- in commit bc3dca0, 23 Jan 2018)

Note: i-t-a or ita is "intended to add".

wt-status.c: handle worktree renames

Before 425a28e (diff-lib: allow ita entries treated as "not yet exist in index" - 2016-10-24, Git 2.11.0-rc0) there are never "new files" in the index, which essentially disables rename detection because we only detect renames when a new file appears in a diff pair.

After that commit, an i-t-a entry can appear as a new file in "git diff-files".
But the diff callback function in wt-status.c does not handle this case and produces incorrect status output.

这篇关于在使用git add-N之后,文件不会进入提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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