本地github ssh多账号问题

哈哈,看来你的 老账号 xxxxx 还在“阴魂不散”,而且你现在用的是 HTTPS 方式 而不是 SSH,所以 SSH 配置对它无效!

别急,我们有两种方法解决:


解决方案 1:改用 SSH 方式(推荐)

既然你已经配好了 SSH 多账号,那就直接 把远程仓库地址从 HTTPS 改成 SSH

  1. 查看当前远程地址(确认是 HTTPS):

    git remote -v
    

    输出可能是:

    origin  https://github.com/xxxxxxxx/xxx.git (fetch)
    origin  https://github.com/xxxxxxxx/xxx.git (push)
    
  2. 修改为 SSH 地址(用你 ~/.ssh/config 里定义的 Host):

    git remote set-url origin [email protected]:xxxxxxxxx/elm.git
    

    (⚠️ github.com-newaccount 要和你 ~/.ssh/config 里的 Host 名一致!)

  3. 再 push 试试

    git push origin main
    

    ✅ 现在应该不会报 xxxxxxx 的权限问题了!


解决方案 2:清除 Git 的 HTTPS 认证缓存(如果仍想用 HTTPS)

如果你 非要继续用 HTTPS,那就得清除 Git 的旧账号缓存:

Windows(Git Credential Manager)

  1. 打开 Windows 凭据管理器(Win + S → 搜索“凭据管理器”)。
  2. 进入 Windows 凭据 → 普通凭据
  3. 找到 git:https://github.com 相关的条目,删除它

Mac / Linux(Git 凭据缓存)

运行:

git config --global --unset credential.helper

或者手动清除缓存:

git credential reject <<EOF
protocol=https
host=github.com
EOF

然后重新 push,会弹出 GitHub 登录窗口

这时输入 新账号的用户名和密码(或 Personal Access Token) 即可。


终极建议:尽量用 SSH!

  • HTTPS 每次 push 都可能要输密码(除非存了 PAT)。
  • SSH 配置好后 一劳永逸,还能多账号切换。

现在再试试 git push,应该没问题了吧?

如果还不行,请提供:

  1. git remote -v 的输出
  2. cat ~/.ssh/config 的内容
  3. 你用的 Git 版本(git --version

我来帮你 精准“捉鬼”

你可能感兴趣的:(SSH,github,ssh,运维)