There’s plenty of information on the Internet on how to mirror a git repository, but for me it was not that clear how to maintain them synced afterwards. Most of posts on stack overflow only mentioned to run git remote update, but they were missing other crucial steps to keep both repositories on sync, so I will detail on this post how to do it.
Creating the mirror repository
First the mirrored repository needs to be created, there’s plenty of information on the net on how to do that. The basic procedure is to use git clone –mirror.
If you don’t have the repository to mirror locally
If this is the case you need to clone the repository to be mirrored with git clone –mirror:
$ git clone --mirror email@example.com:user/repo.git $ cd repo $ git push --mirror firstname.lastname@example.org:user/mirroredrepo.git
If you already have a local copy
On this case then only the git push –miror command is needed, and there are some variations if you don’t need to mirror information like pull requests and such, but this is the basic procedure.
This is the part that wasn’t that clear to me on how to proceed with the updated between the two repositories later on, but the procedure is fairly simple. To have any updates done on the original repository on the mirrored one they need to be pushed from the original repository into the mirror. So basically you need to run again the git mirror –push command described above each time you want to sync both repos:
$ git push --mirror email@example.com:user/mirroredrepo.git
To make this command cleaner, a remote to the mirrored repository can be added:
$ git remote add upstream firstname.lastname@example.org:user/mirroredrepo.git
$ git push upstream --mirror
Then, on the local copy of the mirrored repo the new changes need to be fetched with git remote update:
$ cd mirroredrepo $ git remote update
After that you can pull or merge the changes they way you like. I personally prefer to pull fast-forward changes to avoid merges and have a cleaner history:
$ git pull --ff
Now both repositories will be on sync. You can automate this process using git hooks if you want, but this is the basic procedure.