DEV Community

Emmanuel Odongo
Emmanuel Odongo

Posted on • Updated on • Originally published at odongo.pl

Pushing to Multiple Git Repos

The problem

Assume that you had a repo that was hosted on github, and you decided that for some reason you would like to have a copy of your repo on gitlab as well. Perhaps the obvious solution would be to add another remote to your git config.

git remote add gitlab git@gitlab.com/username/my-repo.git
Enter fullscreen mode Exit fullscreen mode

Pushing to both repos would then be achieved as follows:

git push origin main
git push gitlab main
Enter fullscreen mode Exit fullscreen mode

Remembering to run both these commands every time you wanted to push your changes seems like a tall ask. The good new is that you can get the desired behaviour with just a single command that's likely already part of your muscle-memory:

git push origin main
Enter fullscreen mode Exit fullscreen mode

The solution

A good place to start implementing our solution to this problem would be to check for existing remotes.

git remote -v
Enter fullscreen mode Exit fullscreen mode

The above command lists out our fetch and push remotes, which may look something like this:

origin  git@github.com:username/my-repo.git (fetch)
origin  git@github.com:username/my-repo.git (push)
Enter fullscreen mode Exit fullscreen mode

For the superstitious amongst us, you can optionally clear and re-add the origin remote.

git remote remove origin
git remote add origin git@github.com:username/my-repo.git
Enter fullscreen mode Exit fullscreen mode

We now have one push and one pull URL. The solution to our problem lies in setting a second push URL as shown:

git remote set-url --add --push origin git@gitlab.com:username/my-repo.git
Enter fullscreen mode Exit fullscreen mode

To wrap up, we then set the upstream branch of our choosing (main in this case).

git fetch origin main
git branch --set-upstream-to origin/main
Enter fullscreen mode Exit fullscreen mode

From now on, whenever we run git push origin main, git will push our changes to both remote repositories (github and gitlab). Fetching or pulling changes from origin will always refer to just the one repo (github).

Bonus points

As a final touch, we can give both of our repo hosts a unique name in case we ever need to explicitly push or fetch from a particular one.

git remote add github git@github.com:username/my-repo.git
git remote add gitlab git@gitlab.com:username/my-repo.git
Enter fullscreen mode Exit fullscreen mode

Once this is done, listing our remotes with git remote -v gives the following output:

github  git@github.com:username/my-repo.git (fetch)
github  git@github.com:username/my-repo.git (push)
gitlab  git@gitlab.com:username/my-repo.git (fetch)
gitlab  git@gitlab.com:username/my-repo.git (push)
origin  git@github.com:username/my-repo.git (fetch)
origin  git@github.com:username/my-repo.git (push)
origin  git@gitlab.com:username/my-repo.git (push)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)