Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Git multiple remote warehouses

Shulou Source: shulou.com Published: 2022-06-03 03:44:34 09月26日 Update

1. preface

   I have been using GitHub to manage my own open source projects for a few years. In the last year, I have updated more and more, and the repositories are getting bigger and bigger. Sometimes I feel that GitHub is too slow, especially recently, I feel more obvious, so I have the idea of finding a domestic code hosting platform similar to GitHub. At the same time, I also want to continuously update the repository on GitHub, so I need a local repository (my own development machine) and multiple remote repositories (Github, code cloud, coding).

2. git config for a remote repository

   My open source project Nebula, an event-driven high-performance TCP networking framework, has a git configuration file.git/config as follows:

[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote "origin"] url = https://github.com/Bwar/Nebula.git fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"] remote = origin merge = refs/heads/master3. Add multiple remote repositories using the git command line

   Add a remote repository named " mirror ":

git remote add mirror https://gitee.com/Bwar/Nebula.git

   After executing this command, the contents of the.git/config file become:

[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote "origin"] url = https://github.com/Bwar/Nebula.git fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"] remote = origin merge = refs/heads/master[remote "mirror"] url = https://gitee.com/Bwar/Nebula.git fetch = +refs/heads/*:refs/remotes/mirror/*

   At this point, there was already one local warehouse and two remote warehouses. Use the following commands to pull and push from and to two remote repositories, respectively.

git pull origin master git pull mirror mastergit push origin master git push mirror master4. Update multiple remote repositories simultaneously with one command

  At present, my open source project only has one contributor (plans to introduce other contributors in December 2018), the main push is less pull, I find it troublesome to enter multiple commands, and one command updates the current branch to two remote repositories at the same time to make me satisfied. So change it, do not use the mirror method above, directly add a url to origin to implement a local repository and multiple remote repositories.

git remote set-url --add origin https://gitee.com/Bwar/Nebula.git

  After executing this command, the.git/config content becomes:

[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote "origin"] url = https://github.com/Bwar/Nebula.git url = https://gitee.com/Bwar/Nebula.git fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"] remote = origin merge = refs/heads/master[remote "mirror"] url = https://gitee.com/Bwar/Nebula.git fetch = +refs/heads/*:refs/remotes/mirror/*

  It doesn't matter whether the " mirror " added before is kept or deleted. At this time, we can update two remote repositories with one command:

git push origin master5. Password-free remote warehouse operation

  To execute remote warehouse operation, it is a troublesome thing to input password. You can avoid such trouble by matching user name and password in url of configuration file and improve operation efficiency. Password-free remote warehouse operation can also be achieved through ssh mode, the following only gives https mode password configuration:

url = https://${user}:${password}@github.com/Bwar/Nebula.git

  Just substitute " ${user} " and " ${password} " in the configuration above with your remote repository username and password.

6. Directly modify git configuration file to achieve multiple remote repositories

  The above command git remote to complete a local repository multiple remote repository configuration, these commands are actually achieved by modifying.git/config, in fact, directly modify the configuration file may be faster, I am directly modify the configuration file to complete. Finally, my multiple remote repositories are configured as follows:

[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true[remote "origin"] url = https://${user}:${password}@github.com/Bwar/Nebula.git url = https://${user}:${password}@gitee.com/Bwar/Nebula.git url = https://${user}:${password}@git.coding.net/Bwar/NebulaBootstrap.git fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"] remote = origin merge = refs/heads/master

  Over.

Tags: Warehouse command configuration multiple password file two update at the same time project trouble input content feeling way more and more face more and more obviously it doesn't matter. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei Redmi Microsoft Apple