Q: How can I add a second Git remote repo to push, so that I can automatically push to two remote re

A: Navigate to your Git project in your command line.

Add the second repository with a different remote name. For example, if your first remote is named origin (which is the default), you can name the second remote second or any other name that is unique:

  • git remote add second https://github.com/user/repo2.git

  1. Now you have two remotes for your repository. You can push your changes to the origin remote:

  • git push origin master

And also to the second remote:

  • git push second master

If you want to automatically push to both repositories at once, you can create a script or you can add a Git alias to your .gitconfig:

[alias]  
pu = "!git push origin master && git push second master"  

With this alias, you just need to type git pu to push to both repositories.

Please note that by default, git pull fetches from your origin remote only. If you want to fetch from the second remote, you will need to do it explicitly:

  • git pull second master

More information on managing multiple remotes can be found in the Git documentation.

However, it's worth noting that while it's possible to do this, it's not considered best practice to have multiple remotes for the same repository where you're pushing to both. It might be better to consider why you need to do this and if there's a better way to achieve your overall goal.

Last updated