GitHub 用户切换配置

GitHub 用户切换配置

步骤 1:生成新的 SSH 密钥

在终端执行以下命令(将 your-new-email@example.com 替换为你的新 GitHub 邮箱):

bash 复制代码
ssh-keygen -t ed25519 -C "your-new-email@example.com" -f ~/.ssh/id_ed25519_newuser

说明:

  • -t ed25519: 使用 Ed25519 算法(推荐,更安全)
  • -C: 添加注释(通常是你的邮箱)
  • -f ~/.ssh/id_ed25519_newuser: 指定密钥文件名(避免覆盖现有密钥)

如果系统不支持 ed25519,使用 RSA:

bash 复制代码
ssh-keygen -t rsa -b 4096 -C "your-new-email@example.com" -f ~/.ssh/id_rsa_newuser

执行后会提示:

  • 输入密码(可选,直接回车表示不设密码)
  • 确认密码

步骤 2:启动 SSH 代理并添加密钥

bash 复制代码
# 启动 SSH 代理
eval "$(ssh-agent -s)"

# 添加新密钥到 SSH 代理
ssh-add ~/.ssh/id_ed25519_newuser
# 或如果是 RSA:
# ssh-add ~/.ssh/id_rsa_newuser

步骤 3:查看公钥内容并添加到 GitHub

bash 复制代码
# 显示公钥内容(复制全部输出)
cat ~/.ssh/id_ed25519_newuser.pub
# 或如果是 RSA:
# cat ~/.ssh/id_rsa_newuser.pub

然后:

  1. 登录你的新 GitHub 账户
  2. 进入 Settings → SSH and GPG keys
  3. 点击 "New SSH key"
  4. Title: 填写一个描述(如 "code project key")
  5. Key: 粘贴刚才复制的公钥内容
  6. 点击 "Add SSH key"

步骤 4:配置 SSH config 文件

创建或编辑 ~/.ssh/config 文件:

bash 复制代码
nano ~/.ssh/config

添加以下内容(根据你使用的密钥类型选择):

如果使用 Ed25519:

复制代码
# 新 GitHub 用户配置
Host github.com-newuser
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_newuser
    IdentitiesOnly yes

# 原 GitHub 用户配置(如果需要保留)
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa  # 或你原来的密钥文件
    IdentitiesOnly yes

如果使用 RSA:

复制代码
# 新 GitHub 用户配置
Host github.com-newuser
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_newuser
    IdentitiesOnly yes

保存并退出(nano: Ctrl+X, 然后 Y, 然后 Enter)

步骤 5:测试 SSH 连接

bash 复制代码
# 测试新用户的 SSH 连接
ssh -T git@github.com-newuser

如果成功,会看到类似:

复制代码
Hi username! You've successfully authenticated...

步骤 6:更新项目的 Git 远程地址

在项目目录中执行:

bash 复制代码
cd /root/code

# 查看当前远程地址
git remote -v

# 更新远程地址(使用新的 Host)
git remote set-url origin git@github.com-newuser:新用户名/code.git
# 将 "新用户名" 替换为你的新 GitHub 用户名

# 验证
git remote -v

步骤 7:配置项目的 Git 用户信息

bash 复制代码
cd /root/code

# 设置本地仓库的用户名和邮箱(仅影响此项目)
git config user.name "你的新GitHub用户名"
git config user.email "你的新GitHub邮箱"

# 验证配置
git config user.name
git config user.email

步骤 8:测试推送

bash 复制代码
# 测试推送(如果有权限)
git push origin master
# 或
git push origin main

常见问题

1. 如果遇到 "Permission denied" 错误

  • 检查 SSH 密钥是否正确添加到 GitHub
  • 确认 SSH config 中的 Host 名称和远程地址匹配
  • 运行 ssh-add -l 查看已加载的密钥

2. 如果需要同时使用多个 GitHub 账户

  • 为每个账户创建不同的 SSH 密钥
  • 在 SSH config 中为每个账户配置不同的 Host
  • 在项目的 remote URL 中使用对应的 Host

3. 查看当前 Git 配置

bash 复制代码
# 查看全局配置
git config --global --list

# 查看本地配置
git config --local --list
相关推荐
峰向AI6 小时前
GenOffice:开源 Office 套件,字节级保留、本地转换、BYOK模式
github
其实防守也摸鱼8 小时前
权限提升与横向移动:从内网渗透到域控的完整技术图谱
运维·服务器·数据库·安全·github·copilot·渗透
m4Rk_8 小时前
【论文阅读】Agent 记忆机制(57):MemSearch-o1——从查询词元生长证据,重组 Deep Search 记忆路径
论文阅读·人工智能·学习·开源·github
其实防守也摸鱼9 小时前
免杀与持久化入门:从载荷免杀到隐蔽通道的完整指南
运维·服务器·数据库·安全·github·copilot·渗透
sec_jay9 小时前
一个面向有限本地语料库的、单智能体、工具调用型 Agentic RAG 原型--关于agent的实践调研报告
github
goplaysource11 小时前
IPTV 频道去重:用哈希解决"同一个频道出现十遍"的问题
github
liuyicenysabel12 小时前
从 0 到 1:一套 GitHub + GHCR + k3s 的全自动 CI/CD 流水线(Flask 项目实战)
ci/cd·flask·github
u13013013 小时前
GitHub 热榜项目:日榜(2026-08-31)
github
Justinsky14 小时前
DeepSeek Harness 开源一个月,我把它整个嵌进了 Electron 桌面软件
github
小宋102114 小时前
MCP 是什么:从零编写一个可供 AI 调用的工具服务
人工智能·github