Git Hook Pull After Push - remote: fatal: Not a git repository: '.'

I tried to set up a git post-update hook on my Raspberry Pi so when I push changes from my laptop, a task running on the Raspberry Pi would stop, pull the updates, and the start again with the new code. I thought it would be as simple as this:

#!/bin/sh

# (stuff to do before the pull)
cd /path/to/my/project
git pull
# (stuff to do after the pull)

This seemed like it should “just work”, but it gave me this error:

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

This felt a bit weird, since the directory most certainly was a git repository; manual pulls worked just fine.

What causes this?

Apparently, in the hook script, git pull uses the GIT_DIR shell environment variable, rather than PWD. Simply cding does not change GIT_DIR, so when you do the git pull, it is using the bare repo’s hooks directory (see this StackOverflow post).

How I fixed it

Change your script to this:

#!/bin/sh

# (stuff to do before the pull)
unset $(git rev-parse --local-env-vars)
cd /path/to/my/project
git pull
# (stuff to do after the pull)

The extra line will parse all of git’s local environment variables and unset them. This way, after you cd and do your pull, it will use that directory’s git repo and settings.