How to associate multiple remote warehouses with Git local warehouse

Usage scenario: The local warehouse needs to submit the code to different remote warehouses. If you want to submit to gitLab and gitee, you need to associate multiple warehouses at the same time

method one: 

To add multiple remote repositories to a local git repository, you can use the following command:

git remote add <remote_name> <remote_url>

Among them, remote_name is the name you want to give the remote warehouse, and remote_url is the URL of the remote warehouse.

For example, if you want to add a remote repository named "origin" and a remote repository named "upstream" to your local repository, you can execute the following command:

git remote add origin <origin_url>
git remote add upstream <upstream_url>

You can use the git remote -v command to view all added remote warehouses and their corresponding URLs.

When you execute the git push command, Git will push the code to the remote warehouse associated with the current branch by default. If you want to push code to a different remote repository, you can use the following command:

git push <remote_name> <branch_name>

Among them, remote_name is the name of the remote warehouse you want to push the code to, and branch_name is the name of the branch you want to push.

For example, if you want to push the current branch's code to the "main" branch of the remote repository named "upstream", you can execute the following command:

git push upstream main

Method 2:

In method 1, since we added multiple remote warehouses, we faced the problem of warehouse selection when pushing and pulling. It is true that this is more rigorous, but in many cases, we only need to keep the remote warehouse exactly the same, without making a distinction, so such a distinction seems a bit "redundant".

Instead of adding additional remote repositories, add additional URLs to existing remote repositories. Use git remote set-url -add to add a remote address to the existing remote warehouse named name, such as

git remote set-url --add origin <origin_url>

Check the associated remote warehouse again:

It can be seen that we did not increase the number of remote warehouses as in method 1, but assigned multiple addresses to a remote warehouse (or to be precise, multiple addresses for push).

Therefore, the push and pull operations after this setting are exactly the same as the original operations, and no adjustment is required.

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/130116543
Recommended