avatar

Catalog
Push a git repositorie to multiple remote Git repositories

Created a project and pushed it to GitHub with the following command. How can I push this project to other remote repositories?

git remote add github https://github.com/zxbetter/test.git
git push -u github master

1. Method 1: Use the git remote add command

1.1 Check the status of the remote repository with the following command. You can see that there is only one remote repository named github.

Code
1
2
3
4
5
git remote
github
git remote -v
github https://github.com/zxbetter/test.git (fetch)
github https://github.com/zxbetter/test.git (push)

1.2 Use the following command to add another remote repository (here, take the code cloud as an example)

git remote add oschina https://git.oschina.net/zxbetter/test.git

1.3 Check the situation of the remote warehouse again, you can see that there are already two remote warehouses. Then use the corresponding command to push to the corresponding repository. The disadvantage of this method is that you have to push twice each time.

Code
1
2
3
4
5
6
7
8
9
git remote
github
oschina

git remote -v
github https://github.com/zxbetter/test.git (fetch)
github https://github.com/zxbetter/test.git (push)
oschina https://git.oschina.net/zxbetter/test.git (fetch)
oschina https://git.oschina.net/zxbetter/test.git (push)

2. Method 2: Use git remote set-url command

2.1 Delete the oschina remote repository in method one.

Code
1
git remote rm oschina

2.2 Use the following command to add a remote repository.

Code
1
git remote set-url --add github https://git.oschina.net/zxbetter/test.git

2.3 View the situation of the remote warehouse. You can see that there are two push addresses on the github remote repository. The advantage of this method is that you only need to push once each time.

Code
1
2
3
4
git remote -v
github https://github.com/zxbetter/test.git (fetch)
github https://github.com/zxbetter/test.git (push)
github https://git.oschina.net/zxbetter/test.git (push)

3. Method 3: Modify the configuration file

Open .git / config to find [remote “github”], add the corresponding url, the effect is as follows. This method is actually the same as the second method.

Code
1
2
3
4
[remote "github"]
    url = https://github.com/zxbetter/test.git
    fetch = + refs / heads / *: refs / remotes / github / *
    url = https://git.oschina.net/zxbetter/test.git

4.About git pull

Methods 2 and 3 are more convenient when pushing. But when pulling, you can only pull code from the first url in method 3. The first method does not have this problem (conflicts may need to be resolved).
Therefore, if only the push operation is performed, methods two and three are recommended, and if the pull operation is also performed, method one is recommended.

Author: John White
Link: https://johnwhite-leaf.github.io/articles/14855bb3/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付宝
    支付宝

Comment