Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Basically, I have a server with a git repo 'origin'. I'm trying to have another repo auto-pull from origin every time someone pushes code to it. I've been using the hooks in origin, specifically post-receive. So far, my post receive looks something like this:

#!/bin/sh
GIT_DIR=/home/<user>/<test_repo>
git pull origin master

But when I push to origin from another computer, I get the error:

remote: fatal: Not a git repository: '/home/<user>/<test_repo>'

However, test_repo most definitely is a git repo. I can cd into it and run 'git pull origin master' and it works fine.

Is there an easier way to do what I'm trying to do? If not, what am I doing wrong with this approach? Thanks in advance.

Edit, to clarify: The repo is a website in progress, and I'd like to have a version of it available at all times that is fully up to date.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I'm using a similar hook, but GIT_DIR must point to the .git subdirectory.

#!/bin/sh
cd /home/<user>/<test_repo>
GIT_DIR=.git git pull origin master
share|improve this answer
    
Just to note, changing into the repository doesn't require that you set GIT_DIR--it gets this automatically be parsing parent directories. –  Andrew M. Feb 7 '11 at 2:23
1  
Have you checked that? I have tested without setting GIT_DIR and it didn't work from post-receive (git 1.7.x afair), probably because GIT_DIR is set there to the repo being pushed to. –  silk Feb 7 '11 at 15:33

When you set an environment variable, its only available within that shell--so when you launch another program, such as git, the local variables won't get passed along. Lets say we have test.sh:

#!/usr/bin/bash
echo "GIT_DIR=$GIT_DIR"

Now lets look at the following examples

$ GIT_DIR=LOOK_MY_GIT_DIR_IS_SET
$ ./test.sh
GIT_DIR=

This is because the environment variable didn't get exported to the launched program.

$ export GIT_DIR=LOOK_MY_GIT_DIR_IS_SET
$ ./test.sh
GIT_DIR=LOOK_MY_GIT_DIR_IS_SET

In ScottZ's example, setting the environment variable when you launch the program causes it to be exported ONLY for that command.

$ GIT_DIR=LOOK_MY_GIT_DIR_IS_SET ./test.sh
GIT_DIR=LOOK_MY_GIT_DIR_IS_SET

Hope this helps!

share|improve this answer
    
Good point, so not only do you need to make sure you are using the .git extension if the repo is non-bare you also need to export the variable in your script. –  ScottZ Feb 6 '11 at 19:19
    
Right; if you wanted, you could always cd into the git repository before running your pull. I.e., cd /home/<user>/<test_repo> && git pull origin master. This might be a little clearer and easier. –  Andrew M. Feb 6 '11 at 22:25
    
You are correct, but not solving the OP question. –  silk Feb 6 '11 at 22:36
    
Thanks! I accepted silk's question because it was more direct, but your explanations were really useful. –  Dane Larsen Feb 6 '11 at 23:36
    
Glad it helped, Dane. :) –  Andrew M. Feb 7 '11 at 2:22

Are you sure you are setting GIT_DIR properly?

I assume you are pulling from a non-bare repo in that case your GIT_DIR should be

GIT_DIR=/home/user/test_repo/.git

To test this run the following on the command line:

GIT_DIR=/home/user/test_repo/.git git status
share|improve this answer

Your Answer

 
discard

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.