Search code examples
gitbashsshpost-update

Problem using Git as a Deployment Tool by branch


I am trying to create a post update script and use git as a deployment tool on a remote server.

My post-update script looks something like this

git update-server-info

for ref in $@;do
                echo $ref
                if [ "$ref"  = "refs/heads/production" ]
                then
                        ssh [email protected] GIT_WORK_TREE=/home/www/test git checkout -f production
                fi
done

However, when I run that, an error comes back that says:

Not a git repository (or any of the parent directories): .git

Which doesn't make any sense because I am able to run git commands from within the directory over ssh. enter image description here

Is there something else that I need to be doing in my post-update script?


Solution

I added the GIT_DIR variable, so now my command looks like:

ssh [email protected] GIT_DIR=/home/www/test/.git GIT_WORK_TREE=/home/www/test git checkout -f production

Solution

  • You also need to set the GIT_DIR environment variable - try the following:

    ssh [email protected] GIT_WORK_TREE=/home/www/test GIT_DIR=/home/www/test/.git git checkout -f production
    

    The rules about GIT_DIR and GIT_WORK_TREE (and their equivalents, --git-dir=<> and --work-tree=<>) are so counter-intuitive to me that I now make it a rule that if I'm setting either one of them, I should set both, and furthermore make sure that they're both set to absolute paths.