git post-receive not working correctly
Asked Answered
T

2

12

I have the following problem. I have updated the 'post-receive' to cd into a certain directory and then pull the repo in to deploy it like so:

#!/bin/bash
cd /var/www/site
git pull origin master

However whenever I do 'git push origin master' on my local machine I get the following:

Counting objects: 5, done.
Delta compression using up to 2  threads.
(etc..)
remote: fatal: Not a git repository: '.'

Yet when I manually cd to /var/www/site and do git pull origin master it works brilliantly.

Transfix answered 28/3, 2012 at 10:45 Comment(0)
L
18

Use unset GIT_DIR as following

#!/bin/bash
cd /var/www/site || exit
unset GIT_DIR
git pull origin master
exec git-update-server-info

You can see more information about GIT_DIR here. Git Loves the Environment

Leukorrhea answered 28/3, 2012 at 10:51 Comment(3)
Brilliant, just what I needed. Do you mind explaining why this needs to be done?Transfix
GIT_DIR is one of a handful of environment variables that you can set for various git commands. In a post-receive hook, $GIT_DIR is always (?) set to .. If you cd elsewhere, git pull still sees $GIT_DIR set to . and expects to find the repo in ., but you've moved and it's not there. Clearing it out makes git go back to its "normal" behavior (looking in ./.git, in the place you cd-ed to).Bravura
Would it not be possible to simply set GIT_DIR to the directory being cded into? It seems redundant to reset GIT_DIR and then separately cd into the working directory if this is what GIT_DIR was designed for. Is there a reason this isn't done?Reiko
W
4

Another option is you can mention the working directory and git directory in the command.

git --work-tree=/home/user/repos/my_app --git-dir=/home/user/repos/my_app/.git <command>

e.g:

git --work-tree=/home/user/repos/my_app --git-dir=/home/user/repos/my_app/.git status
Wanting answered 24/8, 2013 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.