更改您的 .git/hooks/pre-commit
到以下内容:
#!/bin/bash
git status
sleep 10000
更改某些文件,但不要使用 git add
暂存它。然后运行git commit -a
.
您应该看到类似以下内容:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: somefile.txt
然后它将等待 sleep
称呼。同时,打开另一个 shell 窗口并运行 git status
。您应该看到:
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: somefile.txt
不同的 git 调用看到不同的索引,预提交钩子(Hook)根据是否使用 -a/--all 调用提交来看到即将提交的索引,但相同的 git 命令其他 shell 不受影响。
我的问题是我有一个脚本,当它作为预提交 Hook 安装时,它会看到索引,就像 git 在另一个 shell 中运行一样,而不是看到预提交 Hook 的索引应该看到。我想让它看到“预提交索引”,或者至少了解索引在不同 shell 中看起来不同的工作原理。
该脚本执行了一些操作(使用 vagrant),其中它不会被视为具有相同的父 PID 或环境,也许这是相关的。
更新:可能与索引与index.lock有关?
最佳答案
git commit -a
会修改索引,可以拒绝。在我看来,在命令完成之前,其尚未验证的索引修改无法显示给任何其他正在执行的命令。我第一次尝试说服 git 作为钩子(Hook)的一部分运行,而不是作为其他执行命令的一部分运行,方法是导出以 GIT_
开头的每个变量(export ${!GIT_* }
)。
关于git - git 如何向预提交 Hook 显示不同的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894126/