git多用户配置

本文详细介绍了如何在一台电脑上为不同的项目配置多个Git账户,包括设置全局和局部用户、生成SSH-key及添加到GitHub和公司仓库的过程。

git多用户配置

场景

在git的使用中,经常会出现这种情况:在公司有一个git账户,github上有个人账户,希望在同一台电脑针对不同的项目使用不同的账号。
此时就需要进行git多用户配置

  1. 创建用户

    • 全局用户
      git config --global user.name "globalUser"
      git config --global user.email "global@email.com"
      
    • 局部用户
      cd 到项目所在目录,或在项目目录打开git bash
      git config user.name "workUser"
      git config user.email "work@email.com"
      
  2. 生成ssh-key
    针对不同账户保存不同文件名

    ssh-keygen -t rsa -C "global@email.com"
    Enter file in whict to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa
    ssh-add ~/.ssh/id_rsa
    
    ssh-keygen -t rsa -C "work@email.com"
    Enter file in whict to save the key (/c/Users/Administrator/.ssh/id_rsa): work_id_rsa
    ssh-add ~/.ssh/work_id_rsa
    
  3. 将生成的ssh-key添加到github和公司git仓库
    查看密钥,注意添加后缀为pub的key

    cat ~/.ssh/id_rsa.pub
    
  4. ~/.ssh目录下添加config文件

    cd ~/.ssh
    vim config
    
    #github global@gmail.com
    host github.com  #别名,随便定 后面配置地址有用
        Hostname github.com #要连接的服务器
        User global #用户名
        IdentityFile ~/.ssh/id_rsa  #密钥文件的地址,注意是私钥
     
    #github work
    host work #别名,随便定
        Hostname 公司git仓库地址
        Port 公司git仓库端口
        User work
        IdentityFile ~/.ssh/work_id_rsa
    
  5. 验证是否成功

    ssh -T git@github.com
    ssh -T git@公司git仓库地址
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值