配置 SSH 管理多个 Git 仓库和以及多个 Github 账号

在工作中,我们可能需要将代码推送到公司内部的自建 Git 代码托管平台(如 GithLab)。与此同时,日常开发过程中,我们也常常需要将代码提交到社区的 Git 代码托管平台(如 Github 等)。提交到 github 又需要管理多个 github 账号

解决

用过~/.ssh/config配置多个不同的 SSH 认证

# 公司github
ssh-keygen -t rsa -C "YourCompanyEmailAdress"
# 输入密钥的名称: id_rsa_company
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_company

# github1
ssh-keygen -t rsa -C "GithubEmail1"
# 输入密钥的名称: id_rsa_github
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_github_1

# github12
ssh-keygen -t rsa -C "GithubEmail2"
# 输入密钥的名称: id_rsa_github
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_github_2


# `~/.ssh/config
# 小号github
Host xx.github.com # 小号
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github_2
    
# 默认github
Host github
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github_1

Host company-git
    User git
    # 替换为公司的 Git 代码托管平台的服务器
    Hostname company-git-repo.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_company

大号不变,小号 clone 使用 host匹配
git clone git@xx.github.com:xxxx/xxx.git

添加

ssh-add ~/.ssh/id_rsa_github_1

仓库级别配置 git 提交信息

git config user.name xxx && git config user.email xxxx@qq.com 

git配置``

# 全局
cat ~/.gitconfig 
# 仓库
cat .git/config

相关推荐

  1. 配置 SSH 管理 Git 仓库以及 Github 账号

    2024-06-16 10:44:05       32 阅读
  2. Windows下配置账号git ssh

    2024-06-16 10:44:05       50 阅读
  3. git 配置账号(码云、github、gitlab)

    2024-06-16 10:44:05       52 阅读
  4. github 账号共享ssh key 的设置方法

    2024-06-16 10:44:05       32 阅读
  5. 配置SSH密钥以访问不同平台(GitHubGitee)

    2024-06-16 10:44:05       37 阅读
  6. git如何配置远程仓库,并且进行切换

    2024-06-16 10:44:05       58 阅读
  7. Git托管平台Git账户配置

    2024-06-16 10:44:05       51 阅读
  8. Git账号本地SSH连接配置方法

    2024-06-16 10:44:05       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 10:44:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 10:44:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 10:44:05       82 阅读
  4. Python语言-面向对象

    2024-06-16 10:44:05       91 阅读

热门阅读

  1. 1527. 患某种疾病的患者

    2024-06-16 10:44:05       41 阅读
  2. springMVC中的注解

    2024-06-16 10:44:05       37 阅读
  3. GitHub每周最火火火项目(6.10-6.16)

    2024-06-16 10:44:05       36 阅读
  4. 从零开始!Jupyter Notebook的安装教程

    2024-06-16 10:44:05       32 阅读
  5. 基于YOLOv5的钢材表面缺陷检测

    2024-06-16 10:44:05       34 阅读
  6. RDF 简介

    2024-06-16 10:44:05       28 阅读
  7. python字符串篇进阶练习

    2024-06-16 10:44:05       30 阅读
  8. 数据结构之B树

    2024-06-16 10:44:05       43 阅读
  9. [xmake]构建静态库和动态库

    2024-06-16 10:44:05       29 阅读