github修改用户名和密码
🧩 一、修改 GitHub 用户名(username)
✅ 1️⃣ 网页修改(推荐)
- 登录 https://github.com
- 点击右上角头像 → Settings
- 左侧栏选择 Account
- 找到 Change username → 输入新用户名 → Update username
🔸 修改用户名不会丢失代码仓库,但:旧的 https://github.com/oldname/repo.git 会自动重定向;但最好更新你本地仓库的远程地址。✅ 2️⃣ 更新本地仓库的远程地址
如果你之前使用:
https://github.com/oldname/myrepo.git 修改后执行:
git remote set-url origin https://github.com/newname/myrepo.git 查看是否成功:
git remote -v 🔐 二、修改 GitHub 密码(或令牌)
GitHub 2021年起 已不再支持密码推送代码,
推送需要使用 Personal Access Token (PAT)。
✅ 1️⃣ 修改 GitHub 登录密码(网页)
- 登录 GitHub → 点击头像 → Settings
- 左侧选择 Password and authentication
- 在 Change password 部分输入旧密码和新密码。
✅ 2️⃣ 修改 Git 推送认证方式(命令行)
如果你用 HTTPS 推送
旧版本可能还保存了密码,现在要改成使用 Token。
- 先生成新 Token
- 进入 https://github.com/settings/tokens
- 点击 Generate new token → classic
- 勾选权限:
repo、read:org、workflow(视情况) - 复制生成的 Token
- Username: 你的 GitHub 用户名
- Password: 粘贴刚才的 Token
再次执行推送:
git push origin main 会提示输入用户名和密码,此时:
在本地仓库中更新凭证:
git credential-cache exitgit credential reject https://github.com ✅ 3️⃣ 如果你使用 SSH 推送
检查 .ssh/config,或重新生成公钥:
ssh-keygen -t ed25519 -C "[email protected]"cat ~/.ssh/id_ed25519.pub 然后将公钥粘贴到:
👉 https://github.com/settings/keys
测试连接:
ssh -T [email protected] 📦 三、常用验证命令
| 检查项 | 命令 |
|---|---|
| 查看当前远程地址 | git remote -v |
| 更新远程地址 | git remote set-url origin <new_url> |
| 清除旧凭证缓存 | git credential reject https://github.com |
| 测试 HTTPS 推送 | git ls-remote https://github.com/<user>/<repo>.git |
| 测试 SSH 推送 | ssh -T [email protected] |