git can be configured to push and pull from many locations simultaneously, allowing you to save your code on two different platforms while keeping only one local copy. Here's how to set it up.
Remote controls, declared
The "remote" for a branch is a URL from where you get local
git repo changes. Your local
git repo is completely yours ̵
Whenever you clone a new repository, the default remote control is set as "origin". You can find the remotes for a given
git repo by running:
git remote -v
This will likely display the URL of your main repository on GitHub or whatever service you use. If you have multiple remotes, they will also be listed here.
But just because
origin is the standard remote control doesn't mean you only have one. Why would you want two remotes? Well, a good use case is AWS's CodeCommit. It is a hosted
git repository and has many integrations with their EC2 compute platform, allowing automated code implementations to be performed on your servers directly from source control.
However, CodeCommit is quite clunky compared to more targeted
git providers like GitHub, GitLab and BitBucket, and doesn't have the same CI / CD integrations that make these providers great. So you have a dilemma: use CodeCommit as your standard solution
git or build your automated code implementation pipeline yourself.
However, with multiple remotes, you can easily push code to a second repository. When you want to update your servers, you can push your primary resource management changes to CodeCommit to start the deployment pipeline.
Setting up multiple remote controls
git in this way is actually quite simple. You add remotes the same way you would push an existing folder, except instead of adding the “origin” remote, rename it.
git remote add
Then, when you want to push to the second remote, add the remote name and branch to your push command:
git push second master
Or change the default remote with
- set-upstream :  git push –set-upstream second master
This is the simplest setup, but requires you to pass either the name of the remote as an argument , or the remote control changes every time.
Really, if you & # 39; If you're using a two-remote control configuration, you probably want a better way to manage push code to your second remote. The best way to handle this in
git is to create a different branch for code pushed to the second upstream, such as implementations to AWS CodeCommit.
You can create a branch with
checkout -b :
git checkout -b deployment
Then add the deployment remote:
git remote add deployment
and get the master branch:
git fetch deployment master
Then you can set the upstream for the current branch by running:
git branch --set-upstream-to = deployment / master
You can repeat this process for any number of branches, making it a great method to keep track of multiple remotes. Remember, however, that this is only a local setup, so if you push this branch to your primary repository, others will not have their copies of the deployment branch configured to use the second remote automatically.
It would be ideal if the second branch is only one way, which means that you only push code, don't pull new code, otherwise you may encounter unexpected conflicts. Furthermore,
git works fine with multiple remote controls.