git 多用户配置

第一步 删除全局用户名和邮箱配置

# 添加全局用户信息
git config --global user.name "用户名"
git config --global user.email "邮箱"
#删除全局用户信息
git config --global --unset user.name
git config --global --unset user.email

第二步 生成 key 并添加至远端

$ ssh-keygen -t rsa -C "github@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): 自定义一个名字
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:K8ZzHA4rrhgHlv7qyP+dAmvpQIq+jPUpbMdjXZncAnE github@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|      . E        |
|       o         |
|  .   .          |
|.+    .oS+       |
|*. . . =*o.      |
|++o.=.*.=.       |
|*=**==o+.        |
|=OBO=o.o         |
+----[SHA256]-----+

第三步 配置 config

文件路径: ~/.ssh/config
不可有注释

Host github.com
    User github用户名
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
Host 192.168.12.5
    User gitlab用户名
    Hostname 192.168.12.5
    Port 122 # 如果不是22则添加此行
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab

第四步 加入密匙管理器

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa_github
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_github:
Identity added: /c/Users/Administrator/.ssh/id_rsa_github (/c/Users/Administrator/.ssh/id_rsa_github)
 
$ ssh-add ~/.ssh/id_rsa_gitlab
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_gitlab:

第五步 clone 一个仓库

user@DESKTOP-7164OA0 MINGW64 /f/github
$ git clone git ssh 地址
Cloning into 'fight-on-java'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes 输入 yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.

第六步 验证

添加一个 commit 并 push

posted @ 2023-06-06 12:09  laolang2016  阅读(163)  评论(0编辑  收藏  举报