跳到主要内容

git配置多个远程仓库

背景说明

有些情况下我们需要将同一套代码推送到多个远程仓库,例如 githubgiteegitlab 等,这个时候就需要进行一些配置了

配置

添加远程仓库

查看当前的远程仓库配置

$ git remote -v
gitee git@gitee.com:pptfz/docusaurus.git (fetch)
gitee git@gitee.com:pptfz/docusaurus.git (push)

添加第二个远程仓库

git remote add github git@github.com:pptfz/docusaurus.git

再次查看查看当前的远程仓库配置

$ git remote -v
gitee git@gitee.com:pptfz/docusaurus.git (fetch)
gitee git@gitee.com:pptfz/docusaurus.git (push)
github git@github.com:pptfz/docusaurus.git (fetch)
github git@github.com:pptfz/docusaurus.git (push)

推送

分别推送

git push gitee master
git push github master

同时推送

编辑 .git/config 写入以下内容

[remote "all"]
url = git@gitee.com:pptfz/docusaurus.git
url = git@github.com:pptfz/docusaurus.git

编辑完成后 .git/config 内容如下

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = pptfz
email = pptfzo@hehe.com
[remote "gitee"]
url = git@gitee.com:pptfz/docusaurus.git
fetch = +refs/heads/*:refs/remotes/gitee/*
[remote "github"]
url = git@github.com:pptfz/docusaurus.git
fetch = +refs/heads/*:refs/remotes/github/*
[remote "all"]
url = git@gitee.com:pptfz/docusaurus.git
url = git@github.com:pptfz/docusaurus.git

使用以下命令同时推送到多个远程仓库

git push all master