96

I'm new to git so I apologize (and please correct me) if I misuse terminology here, but I'll do my best.

I'm trying to set up a bare git repo (hub) and a development site working copy (prime) on a web server. I've tried to pattern it after this article. I want the development working copy to be updated whenever the hub repo is pushed to. I'm under the impression that the proper hook for this is post-update, which I have created like so:

#!/bin/sh
whoami
cd /path/to/working-copy/
RET=`git pull`
echo $RET

Update

When I push changes from my local repo to the bare hub I get the following output from the post-update script:

remote: sites
remote: fatal: Not a git repository: '.'

However if I SSH into the server as user 'sites' and execute this script manually it works great Any ideas as to what might be going wrong with this hook or script?

Ty W
  • 6,694
  • 4
  • 28
  • 36

5 Answers5

193

Here is the script that ultimately worked. I think the bit I was originally missing that prevented it from working remotely was the unset GIT_DIR

#!/bin/sh
cd /path/to/working-copy/ || exit
unset GIT_DIR
git pull repo branch

exec git-update-server-info
Ty W
  • 6,694
  • 4
  • 28
  • 36
  • 19
    I can confirm, unsetting `GIT_DIR` fixes the problem. – jmtd May 20 '11 at 16:21
  • 17
    For those willing the understand why: it's because git uses the variable `GIT_DIR` instead of `PWD`. `cd`-ing changes the `PWD` not the `GIT_DIR`. There must be a fallback in git from `GIT_DIR` to `PWD` should the first not be available. – zupa Jun 21 '13 at 13:50
  • The question is why must git see it necessary to do this for the hook script (set GIT_DIR so that even the script has 'cd' elsewhere 'git whatever' will still work)? I personally would opt that git does not do this kind of magic. – Steven Haryanto Jul 06 '13 at 05:08
  • 2
    The reason behind this is that Git is applying [some environment variables](https://www.kernel.org/pub/software/scm/git/docs/#_environment_variables) to _all_ core git commands. – Casey Sep 12 '13 at 07:31
  • What happens when there's a merge conflict after `git pull`? – lolololol ol Mar 07 '18 at 14:55
  • After doing this I get `remote: fatal: This operation must be run in a work tree` in a post-receive hook, any advice? – szx Dec 23 '18 at 07:08
30

Try instead:

#!/bin/sh
cd /path/to/working-copy/
env -i git pull
0

Despite that unset GIT_DIR just works.

the problem occurs when you set GIT_DIR wrongly somewhere else.

you can just add that instead: GIT_DIR=.git/ It will work

0

In my case I had specified a working tree, and this breaks on some commands, like pull (or more precisely fetch).

To unset the working tree if it is in your git config is via:

git config --unset core.worktree

(There are other ways to set a work tree)

Important to note,

There is next to no change of this being your problem unless you yourself dug this hole around you by using a custom worktree in the first place.

Banter:

This implies to me that git internals use paths relative to the worktree + .git/ in some cases. In my experience worktrees are not well supported whatsoever, except by the most fundamental parts of git. I have not experimented thoroughly, Git would probably behave if I set whatever the git directory config variable is properly, which I have not played with.

Community
  • 1
  • 1
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
-1

You probably have a permissions issue. I'm not sure how you have setup your bare git repo, but if it's running under the git user, make sure that git user is allowed to perform the git pull in your project directory.

Optionally try this to find out what user you are when the hook is run:

echo `whoami`
Ariejan
  • 10,910
  • 6
  • 43
  • 40
  • if I ssh into the server as the git user I am able to use git pull in my project directory with no problems. I *believe* that is the user that should be executing the script. I can try adding the whoami line to the script, but where does the output from that script go when it is executed as a hook? Maybe I need to send that output to a log file instead of echoing? – Ty W Oct 28 '10 at 14:11
  • What does `echo \`whoami\`` do that `whoami` does not? – Christoffer Hammarström Oct 28 '10 at 14:21
  • Output to stdout from a hook script is visible to the remote doing the action. Or, in other words, if you say 'git push', output from a hook on the remote will show up in your stdout. – ebneter Oct 28 '10 at 20:29