Git replication: Part 1 - The poor man's way

There's a lot of ressources about that on the web, it's a well covered topic already.
Still I wanted to give out my own way of keeping Git repositories synchronized.

Main features:
  • It's cheap :)
  • It doesn't scale
  • Supports pushing/deleting branches and tags
  • Uses Git remote to know the remote repositories, no config file needed
Prerequisites:
  • You've set your remotes correctly in your - bare - repository
  • You've set your SSH key(s) as needed
Installation:

cp <the following script> <your Git repo>/hooks/post-receive && chmod +x <your Git repo>/hooks/post-receive

The hook:
#!/bin/bash
# Read commit values
read oldrev newrev refname
echo "post-receive"

# Is the action a delete ?
if [ $newrev == "0000000000000000000000000000000000000000" ]; then
    PUSH="push --delete"
else
    PUSH="push"
fi

# Get out if there's no remote set
REMOTE=$(git remote)
[[ -z $REMOTE ]] && echo "!! No remote set" && exit 0

# Else loop and push to them
for remote in ${REMOTE}; do
    git $PUSH $remote $refname
done

So you get it as it's looping over the remotes, having more than 2 or 3 will seriously make long pushes... Still works like a charm to keep an external copy of your repository.