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.
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.
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.
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.