git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
git add --interactive newfile
简单的演示:
mkdir /tmp/demo
cd /tmp/demo
git init .
echo hello > newfile
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
- 提示如果您确定git对象数据库中已存在“空” blob,则可以对哈希进行硬编码
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391。我不建议这样做
- 提示如果您使用的是Windows,则可以使用
NUL:代替/dev/null。否则,使用类似echo -n '' | git hash-object --stdin -w
现在索引将包含newfile为空blob,并且如果尚未存在该空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
这应该正是您想要的。我是否也可以建议使用vim逃犯插件进行非常智能的索引管理(请参阅更好的git add -p?)
</shame>