Configuring Git with More than One Remote Repository

Git Remote

A branch’s “remote” is a URL from which your local git repo fetches changes. Your local git repo is entirely yours—other people’s code has no effect on it until they push their commits to the remote. Hopefully, you’re all using the same remote and everything is in sync, but the remote is just a point of contact. You could easily clone that endpoint and switch to a new remote.

The default remote is “origin” when you clone a new repository, and you can find the remotes for any given git repo by running:

git remote -v

This will most likely display the URL of your main GitHub repository or whatever service you’re using. If you have multiple remotes, they will appear here as well.

Set Up Multiple remotes

This method of using git is actually quite simple. You add remotes in the same way you would push an existing folder, except instead of adding the “origin” remote, you rename it.

git remote add <name> <url>

When you’re ready to push to the second remote, include the remote name and branch in your push command.

git push second master

Alternatively, use –set-upstream to change the default remote:

git push --set-upstream second master

This is the most basic configuration, but it requires you to either pass the remote name as an argument or switch the remote every time.

You can make a branch by using checkout -b:

git checkout -b deployment

After that, add the deployment remote:

git remote add deployment <url>

and locate the master branch:

git fetch deployment master

Then, run the following command to set the upstream for the current branch:

git branch --set-upstream-to=deployment/master

You can repeat this process for any number of branches, making it an excellent way for keeping track of several remotes. Remember, though, that this is simply a local arrangement, so if you push this branch to your primary repository, others won’t have their copy of the deployment branch configured to use the second remote automatically.

It would be best if the second branch is purely one way, meaning, you are only pushing code, not pulling new code, else you may run into unwanted conflicts. Aside from that, git works flawlessly with numerous remotes.


Leave a Comment

Your email address will not be published. Required fields are marked *