zoukankan      html  css  js  c++  java
  • 配置GitHub和 Gitee共存环境

    配置GitHub 和Gitee共存环境



    前言

    • Git共有三个级别的config文件,分别是system、globallocal

    • 在当前环境中,分别对应

      • %GitPath%mingw64etcgitconfig 文件

      • %RepoPath%.gitconfig 文件

      • $home.gitconfig 文件

        note: 其中%GitPath%为 Git的安装路径,%RepoPath%为某仓库的本地路径。

    • 所以 system 配置整个系统只有一个,global 配置每个账户只有一个,而 local 配置和git仓库的数目相同,并且只有在仓库目录才能看到该配置。


    准备

    1. git工具: https://git-scm.com/downloads

    2. GitHub账号: https://github.com

    3. Gitee账号: https://gitee.com


    配置

    1. 清除 git 的全局设置

      • 查看全局变量

        git config --global --list
        
      • 清除全局user.nameuser.email

        git config --global --unset user.name
        git config --global --unset user.email
        

        note: 此方案适合只使用GitHub 或Gitee(可以试试GitHub和 Gitee使用同一个账号)

    2. 生成并添加 SSH Keys

    3. 多环境配置config文件

      • ~/.ssh/目录下创建config文件

        touch ~/.ssh/config

      • 配置config文件内容

        • 最简配置

          # GitHub
              Host github.com
              HostName github.com
              IdentityFile ~/.ssh/id_ed25519
          
        • 完整配置

          # Default gitHub user Self
              Host github.com
              HostName github.com
              User git
              PreferredAuthentications publickey
              IdentityFile ~/.ssh/id_ed25519
              AddKeysToAgent yes
          
          # Add gitee user
              Host gitee.com
              HostName gitee.com
              User git
              PreferredAuthentications publickey
              IdentityFile ~/.ssh/id_ed25519
              AddKeysToAgent yes
          
        • 参数解释

          • Host

            它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。
            这里可以使用任意字段或通配符。
            当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。
            
          • Port

            自定义的端口。默认为22,可不配置
            
          • User

            自定义的用户名,默认为git,也可不配置
            
          • HostName

            真正连接的服务器地址
            
          • PreferredAuthentications

            指定优先使用哪种方式验证,支持密码和秘钥验证方式
            
          • IdentityFile

            指定本次连接使用的密钥文件
            
          • AddKeysToAgent yes

            将私钥加载到 ssh-agent,
            等同于 ssh-add ~/.ssh/id_ed25519
            

    检验

    1. clone 测试

      • GitHub 项目

        $ git clone git@github.com:librarookie/spring-boot.git
        
        Cloning into 'spring-boot'...
        remote: Enumerating objects: 15, done.
        remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
        Receiving objects: 100% (15/15), done.
        
      • Gitee 项目

        $ git clone git@gitee.com:librarookie/test.git
        
        Cloning into 'test'...
        remote: Enumerating objects: 19, done.
        remote: Counting objects: 100% (19/19), done.
        remote: Compressing objects: 100% (9/9), done.
        remote: Total 19 (delta 0), reused 0 (delta 0), pack-reused 0
        Receiving objects: 100% (19/19), done.
        
    2. push 测试

      • commit

        $ git commit -am "test"
        
        Author identity unknown
        
        *** Please tell me who you are.
        
        Run
        
        git config --global user.email "you@example.com"
        git config --global user.name "Your Name"
        
        to set your account's default identity.
        Omit --global to set the identity only in this repository.
        
        fatal: unable to auto-detect email address (got 'noname@G3.(none)')
        
        • 原因是没有配置 user.nameuser.email

          • 方案一: 设置全局变量的 user.nameuser.email
          git config --global user.email "you@example.com"
          git config --global user.name "Your Name"
          
          • 方案二: 设置项目库局部 user.nameuser.email
            1. 进入项目本地仓库
            2. 设置 user.nameuser.email
          git config --local user.email "you@example.com"
          git config --local user.name "Your Name"
          
      • commit 2

        $ git commit -am "5555" 
        
        [test 52bcd83] 5555
        1 file changed, 1 insertion(+)
        
      • push

        $ git push 
        
        Enumerating objects: 5, done.
        Counting objects: 100% (5/5), done.
        Writing objects: 100% (3/3), 242 bytes | 242.00 KiB/s, done.
        Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
        remote: Powered by GITEE.COM [GNK-6.1]
        To gitee.com:librarookie/test.git
        a14d3de..52bcd83  test -> test
        

    拓展



    Reference

  • 相关阅读:
    [译]ABP vNext微服务演示,项目状态和路线图
    [译]初试C# 8.0
    [译]ABP vNext介绍
    ViewModel从未如此清爽
    python 函数基础及装饰器
    python 基础一
    scrapy基础二
    scrapy 基础
    python 基础技巧
    pandas 基础
  • 原文地址:https://www.cnblogs.com/cure/p/15390709.html
Copyright © 2011-2022 走看看