git 实现一份代码push到两个git地址上

  • 时间:2025-10-22 18:02 作者: 来源: 阅读:10
  • 扫一扫,手机访问
摘要:一直以来想把自己的博客代码托管到github 和coding上 想一次更改 一次push 两个地址一起更新 今天有空查资料 实践了下 本博客的 github地址 coding的git地址如果是 Git,一般来说最佳方法是给 origin 设两个地址:Use case 1: 多地址的 remote repo:git remote set-url origin


一直以来想把自己的博客代码托管到github 和coding上 想一次更改 一次push 两个地址一起更新 今天有空查资料 实践了下

本博客的

github地址

coding的git地址

如果是 Git,一般来说最佳方法是给 origin 设两个地址:

  • Use case 1: 多地址的 remote repo:

git remote set-url origin --add https://coding.net/u/niefengjun/p/blog_es6/git

git remote set-url origin --add https://github.com/niefengjun/boke

在 .git/config 里得到

...

[remote "origin"]

url = https://coding.net/u/niefengjun/p/blog_es6/git

url = https://github.com/niefengjun/boke

...

[branch "master"]

remote = origin

...

然后

git push origin master

就会同时提交到两个 repo,而

git pull origin master

会从两个 repo 里取得更新。

当然 URL 和 repo 不必定非要是 GitHub 上的,具有两个 url 的 remote 也不必定要是 origin,列如可以设置成 all。

  • 只用于 push 的备份 repo

另外一种 use case,你想从 repo1 pull,但是 push 的时候要推送到 repo1 和另一个 repo2,

git remote set-url origin --add https://coding.net/u/niefengjun/p/blog_es6/git

git remote set-url origin --push --add https://github.com/niefengjun/boke

在 .git/config 里得到

...

[remote "origin"]

url = https://coding.net/u/niefengjun/p/blog_es6/git

pushurl = https://github.com/niefengjun/boke

...

[branch "master"]

remote = origin

...

然后

git push origin master

就会同时提交到两个 repo,而

git pull origin master

会从两个 GitHub repo1 里取得更新。

有时候第一个git 已经开发了 才想起来增加第二个 这个的话 会被告知 第二个本地没有分支

error: failed to push some refs to 'git@github.com:niefengjun/boke.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes

hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这个时候用命令

git push -f origin master

即可完成提交

至此后来提交的话 会在两个git 上看到我的更新代码

http://www.niefengjun.cn/blog/0277690638201db4b26b608a09900a5e.html

  • 全部评论(0)
手机二维码手机访问领取大礼包
返回顶部