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
相关推荐
粟悟饭&龟波功2 小时前
【GitHub热门项目】(2025-12-08)
github
逛逛GitHub2 小时前
这个月狂揽 1.8 万 Star!这个 GitHub 项目破解了 AirPods。
github
谅望者3 小时前
从 GitHub Copilot 到 Claude Code:AI 编码的 3 年演变之旅
人工智能·github·copilot
齐齐大魔王3 小时前
系统安装概述
编辑器·github
Web极客码5 小时前
如何在WordPress网站中添加Cookie弹窗
前端·安全·github·wordpress
向阳是我5 小时前
v0.app的next.js项目自动部署到宝塔服务器教程
服务器·开发语言·javascript·github·ai编程
猫头虎-人工智能6 小时前
openEuler远程批量部署实战 SSH VNC IPMI全流程解析
运维·git·开源·ssh·github·开放原子·开源软件
Jul1en_6 小时前
解决 GitHub Actions 同步 Gitee 仓库中遇到的一些问题
ci/cd·gitee·自动化·github