Ya' row ze: Lets go!

You are here

using a git repo to keep multiple locations in sync

Here's a fun script I created to demonstrate how to use a git repo to keep multiple target servers in sync:


# create a master repo that external users can push/pull from
git init --bare masterrepo.git

#create a file that will list known remote locations
touch masterrepo.git/hooks/remotelist.txt

#create a post-receive hook that knows how to use this file and push code to the target
cat > masterrepo.git/hooks/post-receive << EOF
while read STR; do
name=\$(echo \$STR | cut -f1 -d '|')
path=\$(echo \$STR | cut -f2 -d '|')

git remote add \$name \$path
git remote update \$name
git push \$name refs/heads/*:refs/heads/*

done > masterrepo.git/hooks/remotelist.txt

# do the same for the second location
git init staging2
git -C staging2 config receive.denyCurrentBranch updateInstead
echo 'staging2|../staging2/.git' >> masterrepo.git/hooks/remotelist.txt

######### as the developer
git clone masterrepo.git developerlocation

#now do normal add/commit/push operations and see what happens to staging1 and staging2

Back to top