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 firstname.lastname@example.org:path/repository.git $ git remote add upstream email@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 firstname.lastname@example.org:path/repository.git (fetch) origin email@example.com:path/repository.git (push) upstream firstname.lastname@example.org:path/repository.git (fetch) upstream email@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 firstname.lastname@example.org:path/repository.git $ git remote set-url --add --push origin email@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 firstname.lastname@example.org:path/repository.git (fetch) origin email@example.com:path/repository.git (push) origin firstname.lastname@example.org: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/.