Calling 'git pull' from a git post-update hook

梦想的初衷 提交于 2019-11-27 11:30:02
VonC

You have various diagnostics to run as suggested in this SO answer.

In particular, check out the the value of GIT_DIR and GIT_WORK_TREE.

While the hook is running, GIT_DIR and (if the worktree can't be inferred from GIT_DIR) GIT_WORK_TREE are set.
That means your pull won't run with the repository in the directory you changed to.


See also blog post Using Git Inside a Git Hook:

Eventually we got our linux guru over and he noticed that the environment under which the git user runs is totally different when inside a hook.
Gitolite does a bunch of things to the env, but the one that was screwing us up was the setting of the GIT_DIR.
After we figured that out, the solution was as easy as:

ENV.delete 'GIT_DIR'

in our ruby script that is triggered by the 'post-receive' hook.


Same deal in Git Tip: Auto update working tree via post-receive hook, but with an elegant way out of this:

The solution?
It turns out the post-receive hook starts out with the GIT_DIR environment variable set to the repo/.git folder, so no matter what path you 'cd' into it will always try to run any following git commands there.
Fixing this is simply a matter of unsetting the GIT_DIR
(thanks to Ulrich Petri for the elegant env -i solution):

#!/bin/sh
cd ..
env -i git reset --hard

How about specifying the --git-dir.

#!/bin/sh
cd /home/git/www/epicac
git --git-dir=.git pull

This should do the trick in just one line:

git -C /home/git/www/epicac pull
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!