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

时间:2011-10-04 09:01:07

标签: git githooks git-checkout

我尝试在每次成功提交后使用以下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。

1 个答案:

答案 0 :(得分:18)

这里的问题是在post-commit钩子中(也是 pre-commitprepare-commit-msgcommit-msgtGIT_INDEX_FILE环境变量设置为.git/index。 (这个 没有在githooks documentation中记录,但我已经记录 张贴在其他地方about the settings of environment variables and the current directory in git hooks。)

描述了GIT_INDEX_FILE环境变量的效果 在git手册页的ENVIRONMENT VARIABLES部分中:

  

<强> 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