Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 260
Andy Baker Avatar asked Aug 27 '10 09:08

Andy Baker


People also ask

How do I mirror a git repository?

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.

What is git mirror command?

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.

What does git push -- mirror do?

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.


1 Answers

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 
like image 139
Manoj Govindan Avatar answered Sep 28 '22 07:09

Manoj Govindan