zoukankan      html  css  js  c++  java
  • 干货!Git 如何使用多个托管平台管理代码

    考虑到github不能免费创建私有仓库原因,最近开始在使用码云托管项目,这样避免了连接数据库的用户密码等信息直接暴露在公共仓库中。今天突然想到一个点,就是能不能同时把代码推送到github和码云上呢?答案是可以的。

    背景

    首先,我们在开始一个项目时,在本地写了一些代码,需要同时托管到github和码云(gitee)上。这个时候我们要怎么办呢?请接着看。

    实现方法

    添加密钥对

    在C:Users obin.ssh目录下运行git bash

    // 这个是给github生成的
    ssh-keygen -t rsa -C "1148121254@qq.com"
    // 这个是给码云生成的
    ssh-keygen -t rsa -C "cumtrobin@163.com"
    

    生成后自行命名管理,这里不再赘述。接着把公钥分别放在github和码云上。私钥可以用config文件管理

    # 配置github.com
    Host github.com                 
        HostName github.com
        IdentityFile C:\Users\robin\.ssh\id_rsa_github
        PreferredAuthentications publickey
        User cumtRobin
    
    # 配置gitee.com
    Host gitee.com
        HostName gitee.com
        IdentityFile C:\Users\robin\.ssh\id_rsa_gitee
        PreferredAuthentications publickey
        User Tusi
    

    接着我们测试一下

    ssh -T git@github.com
    ssh -T git@gitee.com
    

    成功则会得到这样的反馈

    gitee连接成功

    创建仓库

    首先是在github和码云上分别创建一个仓库。这个玩过github的都知道,不细说。

    接着在本地项目根目录创建git仓库

    git init
    

    本地与remote关联

    要把两个remote仓库与本地git仓库关联起来,我们直接来运行

    // 添加github的远程库
    git remote add origin git@github.com:cumtRobin/BlogFrontEnd.git
    // 添加码云的远程库
    git remote add gitee git@gitee.com:tusi/BlogFrontEnd.git
    

    然后我们运行git remote查看添加的远程库列表

    git remote
    // 得到以下值
    origin
    gitee
    

    说明已经添加成功,接着我们分别查看git status,会看到本地有很多文件待提交,接着git add, git commit,最后git push的时候要注意分开push

    // push到github主分支
    git push origin master
    // push到gitee主分支
    git push gitee master
    

    虽然麻烦了一点,需要push两次,但是目的是初步达成了。如果想要一次性push解决,那也不是没有办法。

    一次性push

    为了避免引起歧义,这里先将origin,gitee的remote库删除

    git remote rm origin
    git remote rm gitee
    

    重新添加remote

    git remote add all git@github.com:cumtRobin/BlogFrontEnd.git
    

    可以看到,我其实是添加的github的远程库,只不过把它的名字叫做all。接着我们把码云上的remote库也关联起来。

    git remote set-url --add all git@gitee.com:tusi/BlogFrontEnd.git
    

    这样操作以后,就可以运行一条push命令了

    git push all --all
    

    有人说可以改.git/config文件实现。其实刚才上面的命令修改的就是config文件,但是本人建议,多练练命令行,这样也会加深对git的理解。这时候我们再查看一下.git/config文件。可以看到remote all下面是有两个url的。

    [core]
    	repositoryformatversion = 0
    	filemode = false
    	bare = false
    	logallrefupdates = true
    	symlinks = false
    	ignorecase = true
    [remote "all"]
    	url = git@github.com:cumtRobin/BlogFrontEnd.git
    	url = git@gitee.com:tusi/BlogFrontEnd.git
    

    学会了两个托管平台的配置,那使用更多的托管平台也就不难实现了。

    ps:再分享一个小技巧,由于我在生成ssh密钥时,加了passphrase,导致我每次push都要输入密码,很烦人。

    其实,只要重置一下这个passphrase就可以了。

    // 进入到.ssh目录,运行git bash
    
    ssh-keygen -p
    
    // 再输入密钥名,如id_rsa_github,先输入旧密码,然后一路回车即可,多个密钥重复此操作即可。
    

    不再需要每次输入密码


    2019-04-18

    1. 补充 git pull 的细节

    因为都是从本地 push 代码到远程仓库,很久没有从远程仓库拉取代码了,今天不小心在 github 上改了仓库中的 readme 文件,导致和 gitee 不同步。使用 git pull 报错,慌的一批。

    $ git pull
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
        git pull <remote> <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=all/<branch> master
    
    

    原来是要使用下面这条命令才行。

    $ git pull all master
    From github.com:cumtRobin/BlogFrontEnd
     * branch            master     -> FETCH_HEAD
    Already up to date.
    
    

    上面的 all 是指 remote ,即远程仓库,master 是指分支名,master 即主干分支。

  • 相关阅读:
    Makefile中的ifeq 多条件使用
    Android引入动态库so的方法
    在Win10上使用Visual Studio2015的Android模拟器
    linux下insmod模块出现“Invalid parameters"
    在干净的ubuntu 14.10上编译Qemu2.2.0的过程
    Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
    C++类中一个构造函数调用另一个构造函数
    用汇编语言角度来理解C语言的一些问题
    TCP协议的安全性分析
    MySQL入门,第四部分,学会创建、删除表
  • 原文地址:https://www.cnblogs.com/wenbinjiang/p/11122228.html
Copyright © 2011-2022 走看看