How to sync a Git project with more than one remote repository
Remote Git repositories are copies of your local project repository hosted on servers somewhere on the Internet. Examples would be the Github server of the Max Planck Institute for Molecular Genetics in Berlin and the Gitlab server of the GWD in Göttingen.
If you didn’t configure anything special, the command git remote
should return at least origin
. For more info, add -v
, for example
$ git remote -v
origin git@github.molgen.mpg.de:bs/macOSnotes.git (fetch)
origin git@github.molgen.mpg.de:bs/macOSnotes.git (push)
origin git@gitlab.gwdg.de:portal.bschmid/macOSnotes.git (push)
displays the remote repositories I have configured for the repository of this website, both associated with the shortcut origin
. More info about a remote repository can be obtained like in this example for the repository of this website:
$ git remote show origin
* remote origin
Fetch URL: git@github.molgen.mpg.de:bs/macOSnotes.git
Push URL: git@github.molgen.mpg.de:bs/macOSnotes.git
Push URL: git@gitlab.gwdg.de:portal.bschmid/macOSnotes.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
Editing the remote configuration
Either add a new remote directly, for example
$ git remote add mynewremote git@github.com:myusername/mygreatproject
adds a new remote named mynewremote
with Git URL git@github.com:myusername/mygreatproject
. If a remote with shortcut mynewremote
already exists and you just want to add a repository for it, use the command
$ git remote set-url --add mynewremote git@github.com:myusername/mygreatproject
Or edit the configuration file with git config -e
and add the new remote there. The configuration file for this website for example reads
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@github.molgen.mpg.de:bs/macOSnotes.git
url = git@gitlab.gwdg.de:portal.bschmid/macOSnotes.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
A very simple setup: Only one remote with the default shortcut origin
, only one branch master
, but two repositories to keep in sync, one in Berlin, one in Göttingen. Using different shortcuts comes in handy when collaborating with different groups you want to share your work with.
Fetching/pulling and pushing
Fetching and pushing involves an additional argument, namely the shortcut of the remote repository which is actually meant, syntaxes are git fetch shortcut
and git push shortcut branch
. Note that git fetch
only downloads any changes from the remote repository since the last fetch or pull but in contrast to git pull
does not try to merge the fetched remote branch into the current local branch.