推送到多个远程托管服务器

参考:Git - Pushing code to two remotes [duplicate]

将代码推送到多个远程托管服务器,多重备份保证安全性,比如推送到githubcoding

有两种情况

  1. 同时推送代码到多个仓库
  2. 指定推送的地址,比如本次操作仅推送到github,不推送到coding

首先查看当前已追踪的远程仓库地址

$ git remote -v
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (push)

当前已追踪了coding服务器上的仓库,需要加入github仓库

针对多个远程仓库的推送,使用SSH传输协议可以提高操作便捷性

同时推送

命令行操作

添加github仓库地址

$ git remote set-url --add --push origin https://github.com/zjZSTU/TEST.git
$ git remote -v
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin  https://github.com/zjZSTU/TEST.git (push)

再重新添加coding的仓库地址

$ git remote set-url --add --push origin https://git.dev.tencent.com/zjZSTU/TEST.git
$ git remote -v
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin  https://github.com/zjZSTU/TEST.git (push)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (push)

这样就可以同时推送到多个远程仓库

$ git push -u origin master 
Username for 'https://github.com': zjZSTU
Password for 'https://zjZSTU@github.com': 
...
...
To https://github.com/zjZSTU/TEST.git
d19a01e..4b76b5d  master -> master
Branch master set up to track remote branch master from origin.
Username for 'https://git.dev.tencent.com': zjZSTU
Password for 'https://zjZSTU@git.dev.tencent.com': 
...
...
To https://git.dev.tencent.com/zjZSTU/TEST.git
d19a01e..4b76b5d  master -> master
Branch master set up to track remote branch master from origin.

文件操作

修改.git文件夹下的CONFIG文件

# 原先
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://git.dev.tencent.com/zjZSTU/TEST.git

# 修改为
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://git.dev.tencent.com/zjZSTU/TEST.git
pushurl = https://github.com/zjZSTU/TEST.git
pushurl = https://git.dev.tencent.com/zjZSTU/TEST.git

origin小节中添加属性pushurl,设置多个远程仓库地址

指定推送

指定推送操作和同时推送操作能够同时并存在同一个仓库中

默认情况下使用origin作为远程仓库名,可以添加另外的名字,在旗下指定新的远程仓库

当前操作如下:默认origin指向coding,添加github指向github仓库,添加all指向githubcoding

命令行操作

添加github远程

$ git remote add github https://github.com/zjZSTU/TEST.git
$ git remote -v
github  https://github.com/zjZSTU/TEST.git (fetch)
github  https://github.com/zjZSTU/TEST.git (push)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (push)

添加all远程

$ git remote add all https://github.com/zjZSTU/TEST.git
$ git remote set-url --add --push all https://git.dev.tencent.com/zjZSTU/TEST.git
$ git remote set-url --add --push all https://github.com/zjZSTU/TEST.git
$ git remote -v
all https://github.com/zjZSTU/TEST.git (fetch)
all https://git.dev.tencent.com/zjZSTU/TEST.git (push)
all https://github.com/zjZSTU/TEST.git (push)
github  https://github.com/zjZSTU/TEST.git (fetch)
github  https://github.com/zjZSTU/TEST.git (push)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin  https://git.dev.tencent.com/zjZSTU/TEST.git (push)

上传代码

# 上传到coding
git push -u origin master
# 上传到github
git push -u github master
# 同时上传
git push -u all master

文件操作

修改.git文件夹下的CONFIG文件,添加githuball小节

[remote "github"]
    url = https://github.com/zjZSTU/TEST.git
    fetch = +refs/heads/*:refs/remotes/github/*
[remote "all"]
    url = https://github.com/zjZSTU/TEST.git
    fetch = +refs/heads/*:refs/remotes/all/*
    pushurl = https://git.dev.tencent.com/zjZSTU/TEST.git
    pushurl = https://github.com/zjZSTU/TEST.git