4

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.

3 Answers 3

2

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
2
  • 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, 2011 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, 2011 at 15:33
3

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!

5
  • 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, 2011 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, 2011 at 22:25
  • You are correct, but not solving the OP question.
    – silk
    Feb 6, 2011 at 22:36
  • Thanks! I accepted silk's question because it was more direct, but your explanations were really useful. Feb 6, 2011 at 23:36
  • Glad it helped, Dane. :)
    – Andrew M.
    Feb 7, 2011 at 2:22
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .