In this article, we are going to learn how to use multiple remote repositories with git. Git is a distributed version control system. Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. When it comes to SCM providers there are many options available. Some of them are GitHub, GitLab, BitBucket, etc. In this article, we are going to learn how to use these three SCM providers with single project to push and pull code.

Creating GitHub Repository

Before you begin this guide you’ll need to create a GitHub repository to store your project. You can create a new repository by clicking the New repository button in your GitHub account. You can name the repository as you wish. In this example, I have named it as multiple-remote-repository-demo. Now you need to clone this repo in to your local directory.

Check Remote Repository

Now you need to check the remote repository details of cloned repo. You can use the following command to check the remote repository info.

1
git remote -v

You will get the following output.

1
2
origin  [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)

Currently you have only one remote repository. That is the GitHub repository. If you check the The YOUR_PROJECT/.git/config file, it has only Github info.

1
2
3
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

Adding Remote Repository

If you need to add the remote repository, we need to use git remote set-url command. You can use the following command to add the remote repository.

1
2
3
4
5
## To add BitBucket remote repository
git remote set-url origin --add [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

## To add GitLab remote repository
git remote set-url origin --add [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

After running the above command, if you check the remote repository info using git remote -v show, you will get the following output.

1
2
3
4
5
$ git remote -v show 
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)

If you check the The YOUR_PROJECT/.git/config file, it has all three remote repository info.

1
2
3
4
5
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

Now you can push your code to all three remote repositories using git push command.

Pull Code to Remote Repository

If you need to pull the code from remote repository, we need to use git pull command. But at the moment, if you run git pull command, it will pull the code from the GitHub repository. If you need to pull the code from BitBucket or GitLab, you need to edit the YOUR_PROJECT/.git/config file. You need to swap the positions of origin urls YOUR_PROJECT/.git/config file.

1
2
3
4
5
6
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
## The url of the remote repository which you need to pull the code should be the first url
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git ## This pull code from GitLab
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

Now it pulls from GitLab instead of Github. If you need to pull from BitBucket, you need to swap the positions of origin urls again.

Let’s assume that you need to pull the code from all three remote repositories. You need to add the remote repository information using git remote.

1
2
3
git remote add github [email protected]:YOUR_USERNAME/YOUR_PROJECT.git 
git remote add gitlab [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
git remote add bitbucket [email protected]:YOUR_USERNAME/YOUR_PROJECT.git

Now if you run git remote -v show command, you will get the following output.

1
2
3
4
5
6
7
8
9
gitlab [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
gitlab [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
bitbucket [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
bitbucket [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
github [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
github [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin [email protected]:YOUR_USERNAME/YOUR_PROJECT.git (push)

If you check the The YOUR_PROJECT/.git/config file, it has all three remote repository info like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
[remote "github"]
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitlab"]
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
fetch = +refs/heads/*:refs/remotes/gitlab/*
[remote "bitbucket"]
url = [email protected]:YOUR_USERNAME/YOUR_PROJECT.git
fetch = +refs/heads/*:refs/remotes/bitbucket/*

If you run git pull, it still pulls from only GitHub. If you need to pull from other remote repositories, you need to use git pull command with remote repository name.

1
2
3
4
5
6
7
8
### To pull from GitHub
git pull origin master

### To pull from BitBucket
git pull bitbucket master

### To pull from GitLab
git pull gitlab master

Conclusion

In this article, we learned how to how to use multiple remote repositories with git. You can find the all the related commands for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.

Happy Coding