专业的编程技术博客社区

网站首页 > 博客文章 正文

Git 客户端多账号管理(git客户端登录)

baijin 2024-10-10 04:23:01 博客文章 13 ℃ 0 评论

前言

在开发的过程中,经常会遇到这样的状况:需要在一台电脑上同时使用两个甚至多个 git 账号,负责不同的用途,比如:一个用来写个人项目,一个用来写公司的项目。为此我们需要为不同的账号生成不同的密钥,那对这些不同的账号和不同的密钥,我们该怎么处理呢?

SSH配置

取消全局设置的用户名和邮箱

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

生成私钥和公钥

 cd ~/.ssh && mkdir -pv {github,company}
$ ssh-keygen -t rsa -C "youremail@example.com"

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa):/Users/Administrator/.ssh/github/id_rsa_github 
# 在回车提示中输入完整路径,如:/Users/Administrator/.ssh/github/id_rsa_github  
#文件命名后,按2次回车,密码为空  
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

$ ssh-keygen -t rsa -C "youremail@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa):/Users/Administrator/.ssh/company/id_rsa_company
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

如果用户家目录中没有 .ssh 目录请自行创建 在这里我创建了两个目录 githubcompany ,分别用来存储不同项目的密钥,进行分类管理

New SSH key

  1. ~/.ssh/github/id_rsa_github.pub 的内容添加到Github的SSH keys中
  2. ~/.ssh/company/id_rsa_company.pub 的内容添加到公司Gitlab的SSH keys中

新密钥添加到SSH agent中

添加新密钥到SSH agent,因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:

$ ssh-add -K ~/.ssh/github/id_rsa_github
$ ssh-add -K ~/.ssh/company/id_rsa_company

如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:

$ ssh-agent bash
$ ssh-add -K ~/.ssh/github/id_rsa_github

使用 ssh-add -l 查看 ssh key 的设置

修改 config 文件

$ vim ~/.ssh/config
Host *
    KexAlgorithms +diffie-hellman-group1-sha1
# default: myfirst
Host github.com
    HostName github.com
    User myfirst
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github/id_rsa_github1
# mysecond
Host mysecond.github.com
    HostName github.com
    User mysecond
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github/id_rsa_github2
Host company.com
    HostName company.com
    User company
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/company/id_rsa_company

其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:mysecond.github.com,那么原地址是:git@github.com:test/Mywork.git,替换后应该是:mysecond.github.com:test/Mywork.git.

测试连通性

$ ssh -T git@github.com

Hi youremail! You've successfully authenticated, but GitHub does not provide shell access.

项目测试

初始化项目a

$ cd ~/a
$ git init
$ echo "myfirst" > README.md
$ git add README.md
$ git config user.name "myfirst"
$ git config user.email "myfirst@gmail.com"
$ git remote add github git@github.com:myfirst/test.git
$ git push -u github master
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:myfirst/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from github.

初始化项目b

$ cd ~/b
$ git init
$ echo "mysecond" > README.md
$ git add README.md
$ git config user.name "mysecond"
$ git config user.email "mysecond@gmail.com"
$ git remote add github git@mysecond.github.com:mysecond/test.git
$ git push -u github master
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To mysecond.github.com:mysecond/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from github.

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表