Git操作之关联多个远程仓库实现一次push多站提交

2,648 阅读3分钟

大家好,我是码路工人。

coder-monkey-blog.jpg

最近使用VuePress搭建了个人博客,
这个之后会放出手摸手教程给大家分享,
先来点花絮暖暖场^_^

  • 构建工具:VuePress(基于Vue、VueRouter、Webpack)
  • 代码托管:GitHub、Gitee
  • 网站托管:GitHub Pages、Gitee Pages

说到这里,明眼人一眼看出问题,怎么代码还分在两个仓库,
Git是“分布式版本管理系统”,不是这个分的意思,两边提交维护多麻烦。
当然,我懂。这还是一份代码,一个本地仓库维护的,
其实很简单,看看怎么做的吧。

首先呢建repo什么的起步动作就不说了,
我们从随便用一个clone到本地说起。

还记得我们的目标吗:

一份本地代码,一次提交同时推送多个远程仓库


已有两个仓库的地址:

github.com/codermonkie… (Clone)

gitee.com/coder-monke…

  • 添加第二个远程仓库的关联:方法一

    git remote add origin https://gitee.com/coder-monkey/v-blog.git
    

    此时如果Gitee上还是空仓库的话,需要:

    git push --set-upstream origin master
    

    如果已建好master分支以上不必执行

    此时执行

    git push
    

    就会分别提交到两个远程仓库了。
    如果不是免密提交的话,
    还会在命令行提示输入用户和密码。
    作为优(Lan)秀(Duo)的程序员,
    这步是一定要省略的,方法下面再说。

  • 方法二,手动编辑.git/config文件
    文件就在项目的根目录

    上面命令执行的结果,其实就是跟我们接下来编辑一样。

    正常clone下来的仓库的.git/config文件会是这样的:

    [remote "origin"]
        url = https://github.com/CoderMonkie/v-blog.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    

    我们只需要在[remote "origin"]url加上一条:
    另一个远程仓库地址,就好了

    [remote "origin"]
        url = https://github.com/CoderMonkie/v-blog.git
        url = https://gitee.com/coder-monkey/v-blog.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    

另一个小目标:实现免密push

  • 方法一:在关联的远程仓库地址中,加入用户名密码

    编辑config文件的origin下的url, 格式具体如下:

    https://username:password@github.com/username/repository.git
    
    • username就是你账号(上例中为Github)
    • password填写密码
    • repository就是你项目仓库名
  • 方法二:保持登录权限凭证

    明文密码会让程序员心里有种隐隐的痛(苦笑),
    所以来用这种吧~

    git config --global credential.helper store
    

    这是给git的全局配置,
    然后在本项目.git/config中添加配置:

    [credential]  
      helper = store
    

    想通过命令的话,将上面命令中的--global去掉即可。 这样,仅第一次push时要用户名密码,
    之后就能愉快地免密推送了。


第三点想说的是,
还记得上面的[remote "origin"]吗,
可以不叫origin
什么,你接触到的远程仓库都叫origin

我是码路工人,你也可以叫我CoderMonkey

origin也只是个别名而已,可以根据方便自己起定

比如,还是这一个本地代码仓库,
同样,还是要推送到两个远程地址,
还能怎么玩?

假设一个git init初始化的本地仓库,
通过git remote add添加远程地址关联:
注意这里哦,不是git remote add origin

git remote add github https://github.com/CoderMonkie/v-blog.git

此时的config

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "github"]
	url = https://github.com/CoderMonkie/v-blog.git
	fetch = +refs/heads/*:refs/remotes/github/*

再添加一个:

git remote add gitee https://gitee.com/coder-monkey/v-blog.git

这时,config文件会变成这样:

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "github"]
	url = https://github.com/CoderMonkie/v-blog.git
	fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
	url = https://gitee.com/coder-monkey/v-blog.git
	fetch = +refs/heads/*:refs/remotes/gitee/*

这时应该这样提交推送:

git push gitee master
git push github master

提交哪个任意指定。还嫌麻烦?

  • 上面两句命令写到批处理,可以双击执行或命令调用
  • 或者配一个package.json里的scripts下的命令

一点都不麻烦。


以上分享,感谢阅读。
优秀的你,get到了吗。


最后,附上博客地址:
gitee/v-blog
github/v-blog

weixin_QRCode

欢迎扫码关注~微信公众号