getting cloned git instances to auto replicate

This is a rather shotgun approach to it but it works.

the general flow:

  1. git server receives an update
  2. git server triggers a shell command on complete
  3. shell command makes a connection to slave server with cloned git with the command to ‘git push’
  4. cloned git is updated and output sent back to user

on the git server edit the following file in your git repo. [git repo directory]/hooks/post-receive

add the following

ssh user@slavegit.server.com ‘./git.sync.sh’

where slavegit.server.com is your server and user is the username that has an ssh authorized_keys already set up (to avoid password prompts)

on the slave server create the following file in the login home directory git.sync.sh withe the following


cd /var/cache/git/[mygitrepo]
git stash -u
git reset --hard origin/master
git pull
git stash drop
cd ~

the git clone needs to be set up to sync ‘git pull’ without being prompted a password.

if you need to clone multiple servers you can add more lines in the post-receive file.

That’s all, you should be able to push changes to the git server, and then the server will update the slave servers

Earlier I mentioned it was rather shotgun styled… it’s because of branches. Syncing branches will trigger needless syncing of slave sites when the main branch is unchanged. There should be nothing disasterous about this, just something of note.

Be the first to comment

Leave a Reply

Your email address will not be published.


*