Git and multiple remotes

Git is the de-facto standard version control system nowadays. It enables developers not only to version their code but also push repositories to a central server. Sometimes it makes sense to push it to more than one remote server.

Add multiple remotes

Many git users know the basic commands to clone, pull and push a repository from a remote git server. This procedure is more flexible then most casual users know. Git allows to add multiple remotes to a repository.

$ git remote add origin git@git.example.com:path/repository.git
$ git remote add upstream git@upstream.example.com:path/repository.git

This syntax allows to add more than one remote to the repository. Per default, git adds the remote “origin” when a new repository is cloned. With the second command shown above, another remote, named “upstream” in this case, can be added.

Checking the remote with the following command reveals the added remote “upstream”. Both remotes, “origin” and “upstream” are set in the repository.

$ git remote -v
origin	git@git.example.com:path/repository.git (fetch)
origin	git@git.example.com:path/repository.git (push)
upstream	git@upstream.example.com:path/repository.git (fetch)
upstream	git@upstream.example.com:path/repository.git (push)

With the remote added, all the git commands like pull and push can be used the same way by using the remote name “upstream” instead of the default remote “origin”. The name for the remote can be freely chosen when adding the remote to the repository.

$ git push --all origin
$ git push --all upstream

The above commands can be used to push the changes to both remotes.

Pushing multiple remotes at the same time

With multiple remotes added to the repository, it is still the responsibility of the user to individually push the changes to both remotes. Pushing to both remotes at once would be more convenient.

To push all the changes to both (or even multiple) remotes at once, git allows to add multiple “push” addresses to one origin.

To do this, the “remote” subcommand allows specifically to add a push address.

$ git remote set-url --add --push origin git@host1.example.com:path/repository.git
$ git remote set-url --add --push origin git@git.example.com:path/repository.git

When the first push address is added, the previous push address is replaced. This means the already existing push address (the one added while cloning the repository) must be re-added to prevent it being lost.

Listing the remotes shows now the origin with multiple push addresses.

$ git remote -v
origin	git@git.example.com:path/repository.git (fetch)
origin	git@host1.example.com:path/repository.git (push)
origin	git@git.example.com:path/repository.git (push)

Now every time changes are pushed to the remote “origin”, the changes get pushed to both of the push addresses specified for it. This of course might introduce more delay when pushing changes but reduces the manual procedure of pushing the changes manually and separately to multiple remote servers.


Read more of my posts on my blog at https://blog.tinned-software.net/.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.