Git同时提交到多个远程仓库

在已经习惯使用git同步写代码,github无疑是最的托管平台,但是国内由于“你懂的”原因,速度很慢,有时无法访问,于是想把自己的代码同步到多个不同的远程仓库备份。

我的主要仓库:

[github] https://github.com/wonux.test.git 主仓库
[oschina] https://git.oschina.net/wonux/test.git 国内常用仓库

另外,国内还有coding(原来的gitcafe合并到了coding),csdn code等。

添加同名多远程仓库

添加一个remote,这里是all,也可以是别的名字(如origin)

git remote add all https://github.com/wonux.test.git

再添加另一个:

git remote set-url --add all https://git.oschina.net/wonux/test.git

重复向同一个远程仓库名字添加需要set-url --add参数

如果有多个,按照上面这一个命令进行添加.

向多远程仓库推送代码
git push all --all

这样就会一次提交到多个库了,上面命令输出如下:

git push all --all
Username for 'https://github.com': wonux
Password for 'https://wonux@github.com': 
Counting objects: 68, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (56/56), done.
Writing objects: 100% (68/68), 72.16 KiB | 0 bytes/s, done.
Total 68 (delta 13), reused 0 (delta 0)
To https://github.com/wonux/test.git
 * [new branch]      master -> master
Username for 'https://git.oschina.net': wonux
Password for 'https://wonux@git.oschina.net': 
Counting objects: 68, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (56/56), done.
Writing objects: 100% (68/68), 72.16 KiB | 0 bytes/s, done.
Total 68 (delta 13), reused 0 (delta 0)
To https://git.oschina.net/wonux/test.git
 * [new branch]      master -> master

记住不要忘记--all参数,如果不加--all,则无法推送,提示:

git push all
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
  git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
  git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
fatal: unable to access 'https://github.com/wonux/test.git/': Couldn't resolve host 'github.com'
分析配置文件

在操作完上面的添加命令后,如果我们打开.git/config文件,我们可以看到这样的配置:

[remote "all"]
	url = https://github.com/wonux/test.git
	fetch = +refs/heads/*:refs/remotes/all/*
	url = https://git.oschina.net/wonux/test.git

因此,直接在.git/config文件中添加:

[remote "all"]
	url = https://github.com/wonux/test.git
	fetch = +refs/heads/*:refs/remotes/all/*
	url = ……

有多少个远程库,就配置多少个url即可.
从这里可以看出,第一种方法生成的配置中还有一个fetch配置,这个配置可以完全去掉.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,可以将一个本地 Git 仓库关联到多个远程仓库。你可以按照以下步骤进行操作: 1. 首先,在本地创建一个空的 Git 仓库,或者进入已有的 Git 仓库目录。 2. 打开终端或命令行界面,使用 `git remote add` 命令来分别关联多个远程仓库。例如,如果你要关联的第一个远程仓库的名称为 "origin",远程仓库的 URL 为 "https://github.com/your-username/your-repo.git",则可以运行以下命令: ``` git remote add origin https://github.com/your-username/your-repo.git ``` 如果你要关联的第二个远程仓库的名称为 "second",远程仓库的 URL 为 "https://github.com/your-username/second-repo.git",则可以运行以下命令: ``` git remote add second https://github.com/your-username/second-repo.git ``` 这里的 "origin" 和 "second" 是远程仓库的名称,你可以根据需要自定义。 3. 确认关联成功后,你可以使用 `git remote -v` 命令来查看已关联的远程仓库列表。例如,运行以下命令: ``` git remote -v ``` 你将看到类似以下输出: ``` origin https://github.com/your-username/your-repo.git (fetch) origin https://github.com/your-username/your-repo.git (push) second https://github.com/your-username/second-repo.git (fetch) second https://github.com/your-username/second-repo.git (push) ``` 这表示你已成功关联了两个远程仓库。 4. 当你需要将代码推送到特定的远程仓库时,可以使用 `git push` 命令加上远程仓库的名称。例如,要将代码推送到 "origin" 远程仓库的 master 分支,可以运行以下命令: ``` git push origin master ``` 要将代码推送到 "second" 远程仓库的 develop 分支,可以运行以下命令: ``` git push second develop ``` 请注意,上述命令中的 "your-username" 应替换为你的 GitHub 用户名。你可以根据需要设置不同的远程仓库名称和分支名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值