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
相关推荐
PaQiuQiu1 天前
GitHub 开源分享 | Coding Interview University
面试·开源·github
毛毛蹭蹭1 天前
github copilot 0.33模型使用问题
github
介一安全1 天前
国内 GitHub 仓库下载提速
gitee·github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-01-17)
ai·开源·大模型·github·ai教程
CryptoRzz2 天前
印度股票数据API对接实战(实时行情与IPO功能全解析)
websocket·区块链·github·共识算法·分布式账本
Aliex_git2 天前
GitHub Copilot 使用笔记
笔记·学习·github·copilot·ai编程
CoderJia程序员甲2 天前
GitHub 热榜项目 - 日榜(2026-1-12)
ai·开源·大模型·github·ai教程
企业对冲系统官2 天前
基差风险管理系统日志分析功能的架构与实现
大数据·网络·数据库·算法·github·动态规划
企业对冲系统官2 天前
基差风险管理系统集成说明与接口规范
大数据·运维·python·算法·区块链·github
企业对冲系统官2 天前
大宗商品风险对冲系统统计分析功能的技术实现
运维·python·算法·区块链·github·pygame