git 配置多端多个账号(码云、github、gitlab)

首先要确认已经安装 Git,可以通过执行 git --version 命令来查看当前安装的版本。

想为同一个电脑配置多个 Git 账户,需要完成以下整体流程:

  1. 清空默认的全局 user.name 和 user.email 配置项;
  2. 为不同的 Git 账户生成不同的 SSH 密钥;
  3. 将以上的 SSH 密钥分别添加到 SSH-Agent 信任列表;
  4. 将以上的公钥添加到相应的 Git 账户中;
  5. 在 config 文件中配置多个 SSH 密钥;
  6. 进行测试。

1、清空默认的全局 user.name 和 user.email

bash 复制代码
git config --global --unset user.name
git config --global --unset user.email

可以通过运行 git config --global --list 命令来查看 Git 的全局配置情况。

2、配置多个 Git 账户的用户名和邮箱:

  1. 单个配置
bash 复制代码
git config --global user.name "yourusername"
git config --global user.email "youremail@email.com"
  1. 多个配置

注意: 这里git config命令没有带---global,表示这是一个局部的设置,即该用户只在当前项目中使用,而不是全局的。

bash 复制代码
git config user.name "1"
git config user.email "1@hotmail.com"
  1. 删除配置
bash 复制代码
git config --unset user.name
git config --unset user.email

3、生成多个 SSH 密钥

管理员打开控制台

  1. 生成 gitte 仓库的 SSH

指定文件路径,方便后面操作:~/.ssh/id_rsa.gitee,id_rsa.github是秘钥的别名。

bash 复制代码
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "xxxxx@qq.com"
  1. 生成 github 仓库的 SSH
bash 复制代码
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "xxxxx@qq.com"

4、将 ssh-key 分别添加到 ssh-agent 信任列表

js 复制代码
ssh-agent bash
ssh-add ~/.ssh/id_rsa.gitee
ssh-add ~/.ssh/id_rsa.github

如果看到 Identitiy added: ~/.ssh/id_ras_github,就表示添加成功了。

5、将公钥添加到相应的 Git 账户中

使用以下命令将公钥复制到粘贴板中,然后粘贴到 Git 账户的 SSH 密钥中。也可以直接打开文件进行复制,文件名带有 .pub 后缀。

bash 复制代码
pbcopy < ~/.ssh/id_rsa.gitee

6、在 config 文件 (在 .ssh 目录下 ) 中配置多个 SSH 密钥

ini 复制代码
#Default gitHub user Self
Host github
    HostName github.com
    User git #默认就是git,可以不写
    IdentityFile ~/.ssh/id_rsa.github
	
# gitee的配置
host gitee 
	Hostname gitee.com #要连接的服务器
	#密钥文件的地址,注意是私钥
	IdentityFile ~/.ssh/id_rsa.gitee

#Add gitLab user 
Host myGit
    HostName git.xxxxx.cn
    IdentityFile ~/.ssh/id_rsa.xxxx

7、进行测试

bash 复制代码
ssh -T git@gitee.com

git clone git@{配置的 Host}:xxx/xxxx.git
git clone git@myGit:xxx/xxxx.git
相关推荐
风娜1 小时前
IT 疑难杂症诊疗室:从现象到根因的系统排查方法论
人工智能·github
曦夜日长6 小时前
Git系统篇,Git本地使用(一):从“究极进化版“到三个区域的底层原理
大数据·linux·git·elasticsearch
怕浪猫6 小时前
2028编程圈很火的10个Skills
算法·面试·github
高频因子挖掘机8 小时前
只取最近 60 个交易日,却每次拉全历史:量化脚本变慢的原因与改法
后端·github·api
宵时待雨9 小时前
GIT版本管理2:远程操作与多人协作
git
鬼手点金9 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
阿里嘎多学长10 小时前
2026-10-12 GitHub 热点项目精选
开发语言·程序员·github·代码托管
伍六琪11 小时前
github 2FA认证教程
github
鸿蒙开发13 小时前
我把自己的应用上线了:从买服务器、域名和 DNS,到备案与 HTTPS
后端·github
邱杰47013 小时前
数智人事系统(十一):Spring Boot + MyBatis-Plus 的 CRUD 落地
后端·github