gitversion-controldvcs

Automatically mirror a git repository


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?


Solution

  • 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.

    UPDATE: If you want to ensure that your server, and your mirror do not get out of sync, and ensure that all branches are also backed up, your post-commit hook can use:

    git push my_remote -f --mirror