使用一句 git 命令將倉庫的改動推送到所有的遠端
- 2020 年 2 月 10 日
- 筆記
git 支援一個本地倉庫包含多個遠端(remote),這對於開源社區來說是一個很重要的功能,可以實時獲取到最新的開源程式碼且能推送到自己的倉庫中提交 pull request。
有時候多個遠端都是自己的,典型的就是 GitHub Pages 服務了,推送總是希望這幾個遠端能夠始終和本地倉庫保持一致。本文將介紹一個命令推送到所有遠端的方法。
我的部落格同時發布在 GitHub 倉庫 https://github.com/walterlv/walterlv.github.io 和 Gitee 倉庫 http://gitee.com/walterlv/walterlv。由於這兩個遠端的 Pages 服務沒有打通,所以我總是需要同時將部落格推送到兩個不同的遠端中。
第一步:設置多個遠端(remote)
使用你平常使用的方法添加多個 git 遠端。
例如:
git remote add github https://github.com/walterlv/walterlv.github.io.git --no-tags
需要注意,對於不是 origin 的遠端,建議不要拉取 tags,所以我加了 --no-tags
選項。
我添加了兩個新的遠端(github 和 gitee)之後,打開你倉庫 .git 文件夾中的 config 文件,應該可以看到如下的內容:
[remote "origin"] url = https://github.com/walterlv/walterlv.github.io.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [remote "github"] url = https://github.com/walterlv/walterlv.github.io.git fetch = +refs/heads/*:refs/remotes/github/* tagopt = --no-tags [remote "gitee"] url = https://gitee.com/walterlv/walterlv.git fetch = +refs/heads/*:refs/remotes/gitee/* tagopt = --no-tags
第二步:添加一個名為 all 的新遠端
現在,我們要添加一個名為 all 的新遠端,並且在裡面添加兩個 url。由於這個步驟沒有 git 命令行的幫助,所以你需要手工修改 config 文件中的內容。
[remote "all"] url = https://github.com/walterlv/walterlv.github.io.git url = https://gitee.com/walterlv/walterlv.git tagopt = --no-tags
如果你有更多需要同步的遠端,那麼就在裡面添加更多的 url。
開始使用一個命令同步所有的倉庫
現在,你可以使用一句命令將本地的修改推送到所有的遠端了。
git push all
我現在自己的部落格倉庫就是這樣的推送方式。於是你可以在以下多個地址打開閱讀我的部落格:
- https://walterlv.com/
- https://blog.walterlv.com/
- https://walterlv.github.io/
- https://walterlv.gitee.io/
- https://walterlv.oschina.io/
本作品採用 知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議 進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名 呂毅 (包含鏈接: https://blog.walterlv.com ),不得用於商業目的,基於本文修改後的作品務必以相同的許可發布。如有任何疑問,請 與我聯繫 ([email protected]) 。