As a programmer, one of the best things that has happened to me is Git! If you don’t know what Git is, you should probably read a paragraph about it before you continue. Git allows you to synchronize the code on your computer with code on a remote repo shared with other developers – usually team members. In this tutorial we will learn to configure one or more Git remotes and pushing code to them with a single command.
git remote add all REMOTE-URL-1
.git remote set-url --add --push all REMOTE-URL-1
.git remote set-url --add --push all REMOTE-URL-2
.git push all BRANCH
– replace BRANCH
 with a real branch name.git fetch --all
.git init
, git pull
, git commit
 and git push
.When you do git init
, you initialize a local Git repository. In general, the purpose is to synchronize this repo with a remote Git repo. To be able to synchronize code with a remote repo, you need to specify where the remote repo exists.
The first step is to add remote repos to your project.
git remote add REMOTE-ID REMOTE-URL
By convention, the original / primary remote repo is called origin
. Here’s a real example:
git remote add origin git@github.com:jigarius/toggl2redmine.git
git remote add upstream git@bitbucket.org:jigarius/toggl2redmine.git
In the above example, we add the remote repository of a project called Toggl 2 Redmine found on GitHub. Use the above command to add one or more remote Git repos – make sure that each repo has its unique ID, i.e. origin
, upstream
in the above example.
Though you can add multiple remotes, usually, each branch of your project can be configured to track a single remote branch. You can setup a branch to track a remote branch as follows:
git checkout BRANCH
git branch -u origin/BRANCH
Here, BRANCH
is the name of the remote branch, which is usually the same as your local branch.
If you want to change the URL associated to a remote that you’ve already added, you can do it with the following command:
git remote set-url upstream git@foobar.com:jigarius/toggl2redmine.git
To see a list of all remotes, simply use the following command:
$git remote -v
origin git@github.com:jigarius/toggl2redmine.git (fetch)
origin git@github.com:jigarius/toggl2redmine.git (push)
upstream git@bitbucket.org:jigarius/toggl2redmine.git (fetch)
upstream git@bitbucket.org:jigarius/toggl2redmine.git (push)
If you’ve added a remote which you no longer require, you can remove it as follows:
git remote remove upstream
Now that you have a primary remote repo and other remotes as well, it’s time to configure the push. The objective is to push to multiple Git remotes with a single git push
command.
To do this, choose a remote ID which will refer to all the remotes. I usually call it all
, but there are developers who prefer origin
. The idea is to add all the remote repo URLs as  “push URLs” to this remote. Here’s what you do:
git remote add all git@github.com:jigarius/toggl2redmine.git
git remote set-url --add --push all git@github.com:jigarius/toggl2redmine.git
git remote set-url --add --push all git@bitbucket.org:jigarius/toggl2redmine.git
If you don’t want to create an extra remote named all
, you can skip the first command and use the remote origin
 instead of all
 in the subsequent command(s).
Now, you can push to all remote repositories with a single command!
git push all BRANCH
It is not possible to git pull
from multiple repos. However, you can git fetch
from multiple repos with the following command:
git fetch --all
This will fetch information from all remote repos. You can switch to the latest version of a branch on a particular remote with the command:
git checkout BRANCH
git reset --hard REMOTE-ID/BRANCH
It is easy to synchronize code between multiple git repositories, especially, pushing to multiple remotes. This is helpful when you’re maintaining mirrors / copies of the same repository. All you need to do is set up multiple push URLs on a remote and then perform git push
to that remote as you usually do.
Rendering context...