我喜欢 git add --interactive.它现在是我日常工作流程的一部分.
问题似乎是它不适用于未跟踪的文件.我想要做的是跟踪一个新文件,但只添加它的一部分,即这个新文件的某些部分尚未准备好上演.
例如,使用git add -i,我可以选择补丁选项,甚至可以编辑单个磁盘以暂存新代码的部分,从而使调试代码注释保持未分级状态.我喜欢这种方式工作,因为它显然我目前正在处理的大型补丁的哪些位置仍然需要工作.
不幸的是,我似乎无法对未跟踪的文件做同样的事情.要么我暂存整个文件,要么没有.我一直在使用的解决方法是在新文件为空时暂存或提交新文件,然后以通常的方式暂存各个更改.但是这个解决方案感觉就像一个肮脏的黑客,当我忘记或改变主意时,它会造成比应有的麻烦.
所以问题是:如何仅仅暂存新文件的一部分,以便跟踪这个新文件但是将其全部或部分内容保持未分级?
Ric*_*sen 390
哇,所有这些update-index和hash-object业务似乎过于复杂.怎么样呢:
git add -N new_file
git add -i
Run Code Online (Sandbox Code Playgroud)
来自git help add:
-N, --intent-to-add
Record only the fact that the path will be added later. An entry
for the path is placed in the index with no content. This is useful
for, among other things, showing the unstaged content of such files
with git diff and committing them with git commit -a.
Run Code Online (Sandbox Code Playgroud)
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
git add --interactive newfile
Run Code Online (Sandbox Code Playgroud)
简单演示:
mkdir /tmp/demo
cd /tmp/demo
git init .
echo hello > newfile
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
Run Code Online (Sandbox Code Playgroud)
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391改为对哈希进行硬编码.我不建议那样做_ NUL:而不是/dev/null.否则,使用类似的东西echo -n '' | git hash-object --stdin -w 现在索引将包含newfile为空blob,如果尚未存在,则将空blob输入到对象数据库中:
$ find .git/objects/ -type f
.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: newfile
#
# 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: newfile
#
$ git diff
diff --git a/newfile b/newfile
index e69de29..ce01362 100644
--- a/newfile
+++ b/newfile
@@ -0,0 +1 @@
+hello
Run Code Online (Sandbox Code Playgroud)
这应该是你想要的.我还可以推荐用于非常智能的索引管理的vim fugitive插件(参见Better git add -p?)
最简单的方法(和imho交互式升级一般)是git gui.它与git捆绑在一起,几乎可以在git支持的所有平台上运行.
只需运行git gui并打开一个gui即可打开和取消暂停状态,甚至单行跟踪和未跟踪文件.
