为什么在提交后挂钩中设置 GIT_WORK_TREE 不起作用?
我尝试使用以下 post-commit
钩子在每次成功提交后部署到特定目录:
#!/bin/sh
export GIT_WORK_TREE=/var/www/example/
export GIT_DIR=/home/mark/test/.git/
git checkout -f
但是,提交后出现以下错误:
$ git commit -m 'An example commit.'
fatal: Unable to create '/var/www/example/.git/index.lock': No such file or directory
[master 0938e48] An example commit.
... 就好像 GIT_WORK_TREE
设置被忽略。为什么设置这个环境变量似乎不起作用?我使用的是 git 版本 1.7.4.1。
I'm trying to use the following post-commit
hook to deploy to a particular directory after each successful commit:
#!/bin/sh
export GIT_WORK_TREE=/var/www/example/
export GIT_DIR=/home/mark/test/.git/
git checkout -f
However, after committing I get the following error:
$ git commit -m 'An example commit.'
fatal: Unable to create '/var/www/example/.git/index.lock': No such file or directory
[master 0938e48] An example commit.
... as if the GIT_WORK_TREE
setting is being ignored. Why does setting this environment variable appear to be not working? I'm using git version 1.7.4.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是在
post-commit
挂钩中(还有预提交
、prepare-commit-msg
和commit-msgt
)GIT_INDEX_FILE
环境变量设置为.git/index
。 (这未记录在 githooks 中
文档,但我已经
在其他地方发布有关环境变量的设置和
git 中的当前目录
hooks。)
描述了
GIT_INDEX_FILE
环境变量的作用在
git
手册页的环境变量部分中,如下所示:...由于某种原因,在这种情况下,
GIT_INDEX_FILE
正在被相对于
GIT_WORK_TREE
使用。要使钩子按您的预期工作,您只需取消设置
GIT_INDEX_FILE
,所以你的钩子看起来像:The problem here is that in
post-commit
hooks (and alsopre-commit
,prepare-commit-msg
andcommit-msgt
) theGIT_INDEX_FILE
environment variable is set to.git/index
. (Thisisn't documented in the githooks
documentation, but I've
posted elsewhere about the settings of environment variables and the
current directory in git
hooks.)
The effect of the
GIT_INDEX_FILE
environment variable is describedin the ENVIRONMENT VARIABLES section of the
git
man page as:... and for some reason, in this situation,
GIT_INDEX_FILE
is beingused relative to
GIT_WORK_TREE
.To make the hook work as you would expect, you just need to unset
GIT_INDEX_FILE
, so your hook would look like: