Often times, you want to duplicate your git repository for redundancy or some other reasons. There are normally two ways of doing this:
1. Making an exact duplicated repository
In this case, you are duplicating your repository to a new location, and therefore you are having two duplicated but really independent repositories. If you want to maintain the synchronization of the two repositories, you will have to execute the same set of push commands separately to the two repositories. So this method is really good for the cases that you are moving your repositories to a new server and going to eventually discard the old repository.
Procedures of making exact duplicated repository are
1.1 bare-clone
Make a bare clone of the repository to be deplicated
$ git clone –bare https://github.com/exampleuser/old-repository.git
1.2 mirror-push
Assume you did above clone in “/home/user/you/github/”, then under this directory, do
$ cd old-repository.git
$ git push –mirror https://github.com/exampleuser/new-repository.git
This will duplicate your repository to a new repository. You can duplicate to a different server by setting a different mirror location. For example, if you want to duplicate to your local host, you can do this by e.g.
$ git push –mirror james@localhost:/media/james/FW/repositories/github/new-repository.git
[Note]: You need to initialize the git repository in your new location if it does not exist by issuing the following commands (assume you are initializing a local repository “new-repository” under you local host “/media/james/FW/repositories/github/”
$ cd /media/james/FW/repositories/github/
$ mkdir new-repository.git
$ cd new-repository.git
$ git --bare init
$ git config core.sharedrepository 1
$ git config receive.denyNonFastforwards true
$ find objects -type d -exec chmod 02770 {} \;
For maintaining multiple mirrored repositories that can be synchronized using simple procedures, you will have to use a different approach as described in 2.
2. Mirroring
If you want mirror a repository in another location, including getting updates from the original, you can clone a mirror and periodically push the changes.
2.1. Make a bare mirrored clone of the repository (Mirror the repository with all branches and tags)
$ git clone –mirror [old-repository-Url], e.g.
$ git clone –mirror https://github.com/exampleuser/repository-to-mirror.git
2.2 Set the push location to your mirror
Assume the cloned local repository mirror is repository-to-mirror.git, and the mirrored repository is james@localhost:/media/james/FW/repositories/github/mirrored-repository.git
$ cd repository-to-mirror.git
$ git remote set-url –push origin james@localhost:/media/james/FW/repositories/github/mirrored-repository.git
or edit the “config” under “repository-to-mirror.git” to have contents as below:
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote “origin”]
url = http://github.com/exampleuser/repository-to-mirror.git
fetch = +refs/*:refs/*
mirror = true
pushurl = james@localhost:/media/james/FW/repositories/github/mirrored-repository.git
This will set the push url of origin to the mirrored repository and keep the pull url of origin to the original repository. Then you can issue the following commands to pull changes from the original repository and push to the mirrored repository in this middle mirrored agent “repository-to-mirror.git”:
$ git fetch -p origin
$ git push –mirror
(original repository) — pull –> (middle agent) — push –> (mirrored repository)
Optionally, you can add a new remote to the new repository (you need to initialize the new git repository), and do the push explicitly.
git remote add remoteName newRepoUrl
2.3 Push all to the new repository (Alternative option)
2.2 shall do all for the mirroring. The following command is another way of pushing to the remote mirror repository.
git push -f –tags remoteName refs/heads/*:refs/heads/*
3. Duplicate only the most up-to-date snapshot
3.1 in you local git workspace
cd path/to/local/repo
3.2 remove the old default remote “origin”
git remote remove origin
3.2 setup the new default remote “origin”
git remote add origin newRepoUrl
3.3 push all branches and tags
git push -u origin –all
git push -u origin –tags
You must be logged in to post a comment.