Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's very weird but when setting a git repository and creating a post-receive hook with:

echo "--initializing hook--"
cd ~/websites/testing
echo "--prepare update--"
git pull
echo "--update completed--"

the hook runs indeed, but it never manage to run git pull properly:

6bfa32c..71c3d2a  master -> master
--initializing hook--
--prepare update--
fatal: Not a git repository: '.'
Failed to find a valid git directory.
--update completed--

so I'm asking myself now, how it's possible to make the hook update the clone with post-receive?

in this case the user running the processes is the same, and its everything inside the user folder so I really don't understand...because if if I go manually into

cd ~/websites/testing
git pull

it works without any problem...

any help on that would be pretty much appreciated

Thanks a lot

share|improve this question
up vote 22 down vote accepted

While the hook is running, GIT_DIR and (if the worktree was defined explicitly) GIT_WORK_TREE are set. That means your pull won't run with the second repository in the directory you changed to.

Try git --git-dir ~/websites/testing/.git --work-tree ~/websites/testing pull; or unset git's repo-local environment with this:

unset $(git rev-parse --local-env-vars)

More info on these environment variables in man 1 git.

share|improve this answer
Thanks a lot for that Tobu, it really works, I will check out more information about it too. Cheers – zanona Jan 29 '10 at 22:32

One thing I experienced was that using the post-update hook '--git-dir' worked great but git was still complaining about a missing working tree (despite using '--work-tree')

In short, this did not work:

git --git-dir /path/to/websites/testing/.git --work-tree /path/to/websites/testing pull

whereas this worked:

cd /path/to/websites/testing
git --git-dir /path/to/websites/testing/.git pull

share|improve this answer

Doesn't this work?

cd /home/smb/websites/testing
env -i git pull


Better still

cd /home/smb/websites/testing
unset GIT_DIR
git pull
share|improve this answer

Have you tried with the full path instead of the ~?

cd /home/smb/websites/testing
git pull
share|improve this answer

The script is probably invoked with /bin/sh, which doesn't understand ~. Try it with the full path to ~ instead.

share|improve this answer

The first recommended answer is right - when a hook is running GIT_DIR etc are set. However, if you want to be able to use the script in another repository then that method is flawed because it hard-codes resetting the GIT_DIR. As they implied, the other way is to remove the variable by using env as follows: env -i git reset --hard Obviously what command you run there is up to you - it's just the 'env -i' bit that's the important bit.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.