为什么在提交后挂钩中设置 GIT_WORK_TREE 不起作用?

发布于 2024-12-07 16:01:46 字数 484 浏览 0 评论 0原文

我尝试使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-12-14 16:01:46

这里的问题是在 post-commit 挂钩中(还有
预提交prepare-commit-msgcommit-msgt
GIT_INDEX_FILE 环境变量设置为 .git/index。 (这
未记录在 githooks 中
文档
,但我已经
在其他地方发布有关环境变量的设置和
git 中的当前目录
hooks
。)

描述了GIT_INDEX_FILE环境变量的作用
git 手册页的环境变量部分中,如下所示:

GIT_INDEX_FILE

此环境[变量]允许指定备用索引文件。如果未指定,则使用默认值 $GIT_DIR/index

...由于某种原因,在这种情况下,GIT_INDEX_FILE正在被
相对于GIT_WORK_TREE使用。

要使钩子按您的预期工作,您只需取消设置
GIT_INDEX_FILE,所以你的钩子看起来像:

 #!/bin/sh
 unset GIT_INDEX_FILE
 export GIT_WORK_TREE=/var/www/example/
 export GIT_DIR=/home/mark/test/.git/
 git checkout -f

The problem here is that in post-commit hooks (and also
pre-commit, prepare-commit-msg and commit-msgt) the
GIT_INDEX_FILE environment variable is set to .git/index. (This
isn'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 described
in the ENVIRONMENT VARIABLES section of the git man page as:

GIT_INDEX_FILE

This environment [variable] allows the specification of an alternate index file. If not specified, the default of $GIT_DIR/index is used.

... and for some reason, in this situation, GIT_INDEX_FILE is being
used 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:

 #!/bin/sh
 unset GIT_INDEX_FILE
 export GIT_WORK_TREE=/var/www/example/
 export GIT_DIR=/home/mark/test/.git/
 git checkout -f
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文