views:

76

answers:

1

One of the side-effects of using an external Subversion repository was getting automatic offsite backups on every commit.

I'd like to achieve the same using Git.

i.e. every commit to my local repository automatically commits to an external one so the two repositories are always in sync.

I imagine that a post-commit hook would be the way to go. Does anyone have any specific examples of this?

+1  A: 

I wrote a post-commit hook for just this purpose. The hook itself is simple; just add a file named post-commit to your .git/hooks/ directory with the following contents:

git push my_remote

The post-commit file should be executable. Also make sure that you add a suitable remote repository with the name my_remote for this this hook to work.

I also made a symlink named post-merge that points to post-commit. This is optional. If you do this you'll auto-sync after merges as well.

Manoj Govindan
Are there any remaining scenarios where the repo could become out of sync?How would I check (and re-sync). Can people commit to the external repo?
andybak
`Can people commit to the external repo?`: this would depend on how the external repo is configured. If you want a read only mirror then you can prevent others from committing. `How would I check (and re-sync)` if no one else is committing to the repo then I don't see too many ways of how the repos could be out of sync. If they do then there is always merge.
Manoj Govindan
Sorry. My additional question was badly worded.
andybak
You might want to use `push -f --mirror` so that it will always succeed (even if there are non-fastforward changes) and so it will be a full mirror with *all* refs. (Otherwise, if you add a new branch, it won't get pushed, since it doesn't exist in the mirror, and the default is to push matching branches.)
Jefromi
@Jefromi: good point about new branches. +1.
Manoj Govindan