美文网首页
git一个项目设置多个远程仓库

git一个项目设置多个远程仓库

作者: okerivy | 来源:发表于2017-02-06 14:01 被阅读603次

    需求很简单

    首先我的远程仓库没有其他分支,只有主分支
    只想在本地修改一份代码后,可以通过终端一键推送到两个远程仓库。

    网上的教程是这样的

    修改.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
    

    两个网站都更新成功

    相关文章

      网友评论

          本文标题:git一个项目设置多个远程仓库

          本文链接:https://www.haomeiwen.com/subject/ufzyittx.html