git一個專案設定多個遠端倉庫

weixin_34290000發表於2017-02-06

需求很簡單

首先我的遠端倉庫沒有其他分支,只有主分支
只想在本地修改一份程式碼後,可以通過終端一鍵推送到兩個遠端倉庫。

網上的教程是這樣的

修改.git/config配置檔案

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = git@github.com:okerivy/xxxx.git
    url = git@git.oschina.net:chatcoin/xxxx.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

出問題

用了上面的方法,結果推送不上去,一直報錯

➜  chatcoinCode git:(master) git push  origin --all

Everything up-to-date
To github.com:okerivy/xxxx.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:okerivy/xxxx.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.

細節問題

首先我的程式碼是 oschina 上的。本地有一份程式碼。
開始的.git/config配置檔案是這樣的

[remote "origin"]
    url = git@git.oschina.net:chatcoin/xxxx.git
    fetch = +refs/heads/*:refs/remotes/origin/*

第一步,需要把本地所有的更改先提交到 oschina 上。

第二步,在 Github 上建立一個空的專案,什麼都不要選。複製 ssh 到 .git/config配置檔案,並刪除 oschina 的 url

[remote "origin"]
    url = git@github.com:okerivy/xxxx.git
    fetch = +refs/heads/*:refs/remotes/origin/*

第三步,把原生程式碼全部強制推送到 Github。

git push -f origin master

第四部,修改.git/config配置檔案,把兩個 url 全部新增

[remote "origin"]
    url = git@github.com:okerivy/xxxx.git
    url = git@git.oschina.net:chatcoin/xxxx.git
    fetch = +refs/heads/*:refs/remotes/origin/*

第五步,修改專案檔案,用終端命令 push 測試下

新增專案
git add .

填寫提交資訊
git commit -m “提交資訊"

提交到伺服器
git push -u origin master

獲取提交的commit資訊
git log

兩個網站都更新成功

相關文章