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

2016-04-15 11:30:20

  一直以来想把自己的博客代码托管到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 上看到我的更新代码

你打算打赏多少钱呢?

打赏
(微信扫一扫)