实际提交陷入困境git commit
是预提交挂钩完成后索引中的任何内容。这意味着你can更改预提交挂钩中的文件,只要您git add
他们也是。
这是我的示例预提交挂钩,从 .sample 修改而来:
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# [snipped much of what used to be in it, added this --
# make sure you take out the exec of git diff-index!]
num=$(cat zorg)
num=$(expr 0$num + 1)
echo $num > zorg
git add zorg
echo "updated zorg to $num"
exit 0
进而:
$ git commit -m dink
updated zorg to 3
[master 76eeefc] dink
1 file changed, 1 insertion(+), 1 deletion(-)
但请注意一个小缺陷(不适用于您的情况):
$ git commit
git commit
updated zorg to 4
# On branch master
# Untracked files:
[snip]
nothing added to commit but untracked files present (use "git add" to track)
$ git commit
updated zorg to 5
# Please enter the commit message for your changes. Lines starting
[snip - I quit editor without changing anything]
Aborting commit due to empty commit message.
$ git commit
updated zorg to 6
# Please enter the commit message for your changes. Lines starting
基本上,因为预提交挂钩更新并且git add
s,即使我实际上没有在这里进行提交,文件也会不断增加。
[Edit2021 年 8 月:我需要强调的是,我do not推荐这种方法。请注意,使用时可能会出现一些奇怪的情况git commit -a
, git commit --include
, and git commit --only
,包括隐含的--only
如果您在命令行上命名文件,则会插入该文件。这是由于这样的事实git commit
创建第二个,有时甚至第三个内部 Git 索引。任何git add
您在钩子内执行的操作只会影响one这两个或三个索引文件。]