Sometimes I need to connect my local code with two remote copies of the same repository to upload the changes on both of them and to maintain both copies updated. It happens when I want to have a copy internal to the company of the code of an external repository to which we had been given temporary access, or during the migration of projects from one GIT server to another, when both servers needed to be updated.
- We need to start from a situation in which the two repositories (the one on gitserverA and the one on gitserverB) are aligned, for example after having locally cloned the repository downloaded from gitserverA and just uploaded it to the gitserverB.
- The development branch is called dev.
At this point, to link the two repositories, we need to associate to a remote repository (called “all“, for example) all the repository you want to link:
git remote add all https://gitserverA/myproject/myproject-backend.git git remote set-url --add --push all https://gitlab.aks.sns.it/myproject/myproject-backend.git git remote set-url --add --push all https://gitserverA/myproject/myproject-backends.git git checkout dev # ... updates to the code and commits ... git push all dev
After these configurations you can work locally, modify the code as you want and then push the changes to both repositories.
Other commands useful in these situations:
# check the current branch git branch # list all the branches git branch -a # list all remotes git remote -v
Sources and references:
-  Git Basics – Working with Remotes, Git-Scm.
-  Managing remote repositories, GitHub.
-  Using git with multiple remote repositories, Michael S. Mikowski.
-  How can I pull/push from multiple remote locations?, StackOverflow.
-  Working with Git remotes and pushing to multiple Git repositories, Jigarius.
-  Git push to multiple remotes, Patrick Connelly.
-  GIT: Schema di funzionamento, Claudio Battaglino.