Git 提交到多个远程仓库

需求

假设我有两个仓库:

git@github.com:everlose/CSS3Practice.git

git@gitlab.com:everlose/CSS3Practice.git

我希望能再一份本地的工程里push代码能同步到两个仓库里。

如果说push代码的指令如这般:git push all -all

命令行操作

输入:git remote add all git@gitlab.com:everlose/CSS3Practice.git

输入:git remote set-url --add all git@github.com:everlose/CSS3Practice.git

提交的时候只要输入:git push all -all。就可以生效。结果如下:

[core]
Saving password to keychain failed
Identity added: /Users/everlose/.ssh/id_rsa (/Users/everlose/.ssh/id_rsa)
Counting objects: 3, done.
Delta compression using up to 4 threads.
[core]
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@gitlab.com:everlose/FEDPractice.git
   643bfce..e0fd8f3  master -> master
Branch master set up to track remote branch master from all.
[core]
Warning: Permanently added the RSA host key for IP address '192.30.252.120' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@github.com:everlose/FEDPractice.git
   643bfce..e0fd8f3  master -> master
Branch master set up to track remote branch master from all.

它的原理同下,都是修改了.git/config下的配置。

直接配置.git/config文件

进入项目的根目录,输入:vim .git/config。

添加如下一行的远程仓库配置,有多少个远程库,就配置多少个url即可。

[remote "all"]  
    url = git@gitlab.com:everlose/CSS3Practice.git 
    url = git@github.com:everlose/CSS3Practice.git

提交的时候只要输入:git push all -all。就可以生效。

参考

原文地址:https://www.cnblogs.com/everlose/p/12826002.html