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.

andy
  • 2,369
  • 2
  • 31
  • 50

2 Answers2

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

Rukmal Dias
  • 3,242
  • 1
  • 34
  • 28
Hasintha Janka
  • 1,608
  • 1
  • 14
  • 27
  • Brilliant, just what I needed. Do you mind explaining why this needs to be done? – andy Mar 28 '12 at 11:59
  • 2
    `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). – torek Mar 29 '12 at 06:15
  • Would it not be possible to simply set `GIT_DIR` to the directory being `cd`ed 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? – Hashim Aziz Sep 12 '19 at 18:22
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
Rukmal Dias
  • 3,242
  • 1
  • 34
  • 28