【发布时间】:2015-03-19 22:06:38
【问题描述】:
我想提交 Python 模块的 __init__.py 文件,该文件在我的磁盘上已经包含代码。但是,对于当前提交,我想将其添加为空,因为这部分代码还有待测试。因此,我使用了
$ git add -N __init__.py
该文件存在于git status 的输出中,但如果我发出git commit,所有其他文件都会进入提交,除了__init__.py,而__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.
有没有办法绕过这个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
【问题讨论】:
-
我相信当您在添加时使用
-N标志时,您需要使用-a运行git commit。来自帮助:这对于使用 git diff 显示此类文件的未暂存内容并使用 git commit -a 提交它们非常有用。 -
这是不行的,因为它会提交所有其他修改过的文件,不管我是否用
git add添加它们。但是,未跟踪的文件仍会以这种方式保持未跟踪状态。 -
太搞笑了。当仅使用
git add -N然后提交时,您可以创建一个完全空的提交。只是想分享那个小宝石。 -
您可以在
git add -N之后使用git add -p并拒绝添加您被询问的单个差异大块。请参阅this answer 了解更多信息。 -
@SvenMarnach 我实际上已经尝试过了,至少在 git 1.9.4 中无法解决问题。
标签: git