mirror of
https://github.com/ZJDoc/GitGuide.git
synced 2026-07-29 15:20:25 +08:00
133 lines
4.4 KiB
Markdown
133 lines
4.4 KiB
Markdown
|
|
# 推送到多个远程托管服务器
|
|
|
|
将代码推送到多个远程托管服务器,多重备份保证安全性,比如推送到`github`和`coding`
|
|
|
|
有两种情况
|
|
|
|
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`指向`github`和`coding`
|
|
|
|
### 命令行操作
|
|
|
|
添加`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`文件,添加`github`和`all`小节
|
|
|
|
[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
|
|
|
|
## 相关阅读
|
|
|
|
* [Git - Pushing code to two remotes [duplicate]](https://stackoverflow.com/questions/14290113/git-pushing-code-to-two-remotes) |