我想提交一个Python模块的__init__.py文件,它在我的磁盘上已经包含了代码.但是,对于当前提交,我想将其添加为空,因为这部分代码尚未测试.因此,我用过
$ git add -N __init__.py
Run Code Online (Sandbox Code Playgroud)
该文件存在于输出中git status,但是如果我发出git commit,则所有其他文件都进入提交,除了__init__.py,依次是依次保留在索引中git status.
该名男子页git-add说的-N那个
Record only the fact that the path will be added later. An entry for the path is placed in the index with no content.
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过这will be added later部分,即添加文件为空而不暂时删除其内容?
编辑:当前(2.2.0)Git会发生这种情况.使用1.7.1和一个小的测试仓库,我收到一个错误:
$ 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
Run Code Online (Sandbox Code Playgroud)
这应该通过Git 2.5(2015年第2季度)来修复,这意味着git commit不会尝试包含"打算稍后添加"的新文件.
请参阅NguyễnTháiNgọcDuy()提交的d95d728pclouds(合并于d0c692263)
diff-lib.c:调整i-t-a条目的位置diff
由"
git add -N" 添加的条目是用户的提醒,以便他们在提交之前不会忘记添加它们.这些条目即使不是真实的,也会出现在索引中.
他们在索引中的存在会导致git status像这样令人困惑的" ":
On branch master
Changes to be committed:
new file: foo
Changes not staged for commit:
modified: foo
Run Code Online (Sandbox Code Playgroud)
如果你做"
git commit",foo即使"status"报告为"to be committed" ,也不会包括" ".
此修补程序将输出更改为
On branch master
Changes not staged for commit:
new file: foo
no changes added to commit
Run Code Online (Sandbox Code Playgroud)
换一种说法:
- 将这些路径视为"尚未添加到索引中,但Git已经了解它们";
- "
git diff HEAD"和"git diff --cached HEAD"不应该谈论它们,而且- "
git diff"应将它们显示为尚未添加到索引中的新文件.
2016年第4季度更新:
commit:修复空提交创建时没有任何更改,但ita条目
(ita或i-t-a"意图添加")
见提交2c49f7f,提交018ec3c,提交b42b451,提交425a28e通过(2016年10月24日)阮泰玉维战(pclouds).
(由Junio C gitsterHamano合并- -在提交6503602,2016年10月27日)
当新的路径被"
git add -N" 添加到索引时,足以绕过检查"git commit"以避免在没有"--allow-empty"的情况下进行空提交.
相同的逻辑阻止" "在" "部分中git status显示"" "这样的路径.new fileChanges not staged for commit
git diff手册页现在包括:
--ita-invisible-in-index:
Run Code Online (Sandbox Code Playgroud)
默认情况下,"
git add -N" 添加的条目在" "中显示为现有空文件,在" "中显示为git diff新文件git diff --cached.
此选项使条目在"git diff"中显示为新文件,在" "中不存在git diff --cached.可以使用此选项还原此选项
--ita-visible-in-index.这两个选项都是实验性的,将来可以删除.
更新2018年第一季度(Git 2.16.x/2.17),git status再次提高.
" git status",在工作树中移动路径(因此使其显示为"已删除"),然后添加-N选项(因此使其显示为"已添加")将其检测为重命名,但未报告新旧路径名正确.
请参阅提交176ea74,提交5134ccd,提交ea56f97,提交98bc94e,提交06dba2b,提交6de5aaf(2017年12月27日)作者:NguyễnTháiNgọcDuy(pclouds).
帮助:Igor Djordjevic(boogisha).
(由Junio C gitsterHamano合并- -在提交bc3dca0,2018年1月23日)
注意:i-t-a或者ita"打算添加".
wt-status.c:处理worktree重命名在425a28e之前(diff-lib:允许将条目视为"索引中尚不存在" - 2016-10-24,Git 2.11.0-rc0)索引中从不存在"新文件",这实际上禁用了重命名检测,因为我们只在新文件出现在差异对中时检测重命名.
在该提交之后,ita条目可以在"
git diff-files"中显示为新文件.
但diff回调函数inwt-status.c不处理这种情况并产生错误的状态输出.
使用Git 2.20更新2018年第四季度,git status避免在同一目标上显示多个重命名.
见Elijah Newren()提交3e73cc6(2018年9月27日).(由Junio C Hamano合并- -在提交98f3f00,2018年10月16日)newren
gitster
commit:修复错误的BUG,'在同一目标上多次重命名?怎么样?'
builtin/commit.c:prepare_to_commit()run_status()如果使用编辑器(包括状态),则可以调用两次,并且用户尝试在没有显式的情况下记录非合并空提交--allow-empty.
如果还涉及重命名(由于使用'git add -N'),则会wt-status.c触发BUG :BUG:wt-status.c:476:在同一个目标上多次重命名?怎么样?
我们遇到这个错误的原因是两个
run_status()调用都使用相同的struct wt_status * (named s),并且s->change在运行之间没有释放.更改插入到s有string_list_insert,这通常意味着第二只运行重新计算所有相同的结果,并覆盖了什么计算的第一次.