用GitHub管理本身的開源項目有幾年了,最近一年更新得比較多,倉庫也愈來愈多愈來愈大。有時候感受GitHub太慢,尤爲是最近感受更爲明顯,因而萌生了再找個國內相似GitHub的代碼託管平臺的想法,同時我也還想持續更新GitHub上的倉庫,因而須要一個本地倉庫(我本身的開發機)多個遠程倉庫(Github、碼雲、coding)。git
個人開源項目Nebula一個基於事件驅動的高性能TCP網絡框架的git配置文件.git/config以下:github
[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
添加一個名爲「mirror」的遠程倉庫:網絡
git remote add mirror https://gitee.com/Bwar/Nebula.git
執行完這條命令後.git/config文件內容變成了:框架
[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/*
此時已是一個本地倉庫,兩個遠程倉庫。使用下面的命令能夠分別從兩個遠程倉庫拉取和推送到兩個遠程倉庫。ssh
git pull origin master git pull mirror master git push origin master git push mirror master
目前個人開源項目只有我一個contributor(計劃2018年12月開始引入其餘contributor),主要push比較少pull,輸入多條命令我都以爲麻煩,一條命令將當前分支同時更新到兩個遠程倉庫才能讓我滿意。因而改變一下,不用上面的mirror作法,直接在origin中添加一個url來實現一個本地倉庫多個遠程倉庫。性能
git remote set-url --add origin https://gitee.com/Bwar/Nebula.git
執行這條命令後.git/config內容變成:fetch
[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/*
以前添加的「mirror」留着或刪掉都不要緊,這時候咱們一條命令便可更新兩個遠程倉庫:url
git push origin master
執行遠程倉庫操做須要輸入密碼是件比較麻煩的事情,在配置文件的url裏配上用戶名和密碼便可免掉這樣的麻煩,提升操做效率。免輸密碼操做遠程倉庫還能夠經過ssh方式實現,下面只給出https方式的免輸密碼配置:.net
url = https://${user}:${password}@github.com/Bwar/Nebula.git
把上面配置中的「${user}」和「${password}」用你的遠程倉庫用戶名和密碼代入便可。命令行
上面經過git remote命令完成一個本地倉庫多個遠程倉庫配置,這些命令實際上都是經過修改.git/config實現的,其實直接修改配置文件可能會更快,我就是直接修改配置文件完成。最後個人多個遠程倉庫配置以下:
[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
完畢。