美文网首页
Git操作多个远程仓库

Git操作多个远程仓库

作者: 你慧快乐 | 来源:发表于2019-06-21 10:39 被阅读0次

    前言

    有时候感觉GitHub太慢,尤其是最近感觉更为明显,于是萌生了再找个国内类似GitHub的代码托管平台的想法,同时我也还想持续更新GitHub上的仓库,于是需要一个本地仓库(我自己的开发机)多个远程仓库(Github、码云、coding)。

    一个远程仓库的git config

    当前git配置文件,在项目的.git/config中:

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = https://github.com/Bwar/Nebula.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    

    添加多个远程仓库

    添加名称为gitee的远程仓库:

    git remote add gitee https://gitee.com/Bwar/Nebula.git
    

    执行完这个命令,我们的配置文件就会变成:

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = https://github.com/Bwar/Nebula.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    [remote "mirror"]
        url = https://gitee.com/Bwar/Nebula.git
        fetch = +refs/heads/*:refs/remotes/mirror/*
    

    此时我们的仓库已经关联了两个远程仓库了,操作仓库时需分别制定名称:

    git pull origin master 
    git pull gitee master
    git push origin master 
    git push gitee master
    

    一条命令同时操作多个远程仓库

    不用上面的mirror做法,直接在origin中添加一个url来实现一个本地仓库多个远程仓库。
    命令:

    git remote set-url --add origin https://gitee.com/Bwar/Nebula.git
    

    也可以直接编辑配置文件,结果如下:

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = https://github.com/Bwar/Nebula.git
            url = https://gitee.com/Bwar/Nebula.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    [remote "mirror"]
        url = https://gitee.com/Bwar/Nebula.git
        fetch = +refs/heads/*:refs/remotes/mirror/*
    

    之前添加的“mirror”留着或删掉都没关系,这时候我们一条命令即可更新两个远程仓库:git push origin master

    相关文章

      网友评论

          本文标题:Git操作多个远程仓库

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