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?
You first have to get the original Git repository on your machine. Then, go into the repository. Finally, use the --mirror flag to copy everything in your local Git repository into the new repo.
Git mirroring is when a mirror copies the refs & the remote-tracking branches. It's supposed to be a functionally identical copy that is interchangeable with the original.
You can git clone --mirror to get a clone of a remote repository with all the information, then take that and git push --mirror it to another location.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With