The basic setup of our Git workflow is a bare repository on a local network server with two developers pushing/pulling to it.
We would like to automagically copy (checkout) each branch that gets pushed to a different location on our local network server. To exemplify:
Pushing the 'develop' branch copies to 'develop' sub-folder. Pushing the 'master' branch copies to the 'master' sub-folder.
The problem we are having is getting the post-receive hook to do this. Here is what we currently have:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
GIT_WORK_TREE=/master
git checkout -f $branch
echo 'Changes pushed master.'
fi
if [ "develop" == "$branch" ]; then
GIT_WORK_TREE=/develop
git checkout -f $branch
echo 'Changes pushed to develop.'
fi
done
The error received is:
'remote: fatal: This operation must be run in a work tree remote: Changes pushed to develop.'
As you would expect from that error - nothing is actually checked out.
I had also tried the post-receive this way but the same issue:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
git --work-tree=/master checkout -f $branch
echo 'Changes pushed master.'
fi
if [ "develop" == "$branch" ]; then
git --work-tree=/develop checkout -f $branch
echo 'Changes pushed to develop.'
fi
done
Can anyone explain what I am doing wrong here (feel free to explain it like you would to a 3-year old :)). Thanks.
To make the answer clearer for future readers, Torek hit it on the head. I was using --work-tree=/master
to try and get to to a folder called 'master' inside the root of my bare repo (e.g. alongside 'branches', 'hooks' etc). As soon as I changed this to --work-tree=./master
(note the dot before the forward slash) everything worked as I expected.
deploy()
snippet you have provided - would that go in the post-receive hook? Sorry for the basic questions but this is all very much over my head at present. – Fecundity