总是遇到奇奇怪怪的问题,这几天自己提交的代码,居然是另一个账号的名字“HarukaMa”。
If you see my name in your commit, then you are using a wrong email address on that commit. Fix that instead.
记录一下:
1. 检查当前的 Git 配置
首先,需要检查 Git 配置,看看 user.name 和 user.email 当前设置的是什么。
检查全局配置:
Bash
git config --global user.name
git config --global user.email检查当前项目配置(在项目根目录下运行):
Bash
git config user.name
git config user.email如果当前项目有本地配置,它会显示出来。如果没有,它会回退到全局配置。
2. 设置正确的 Git 用户名和邮箱
如果发现 user.name 不是想要的名字,或者 user.email 不是自己的 GitHub 邮箱,需要重新设置它们。
全局设置(推荐,影响所有 Git 仓库):
Bash
git config --global user.name "真实姓名或希望显示的名字"
git config --global user.email "GitHub注册邮箱"例如:
Bash
git config --global user.name "Your Name"
git config --global user.email "your_github_email@example.com"项目级设置(只影响当前仓库):
如果只希望当前项目使用特定的名字/邮箱,可以去掉 --global 选项,在项目目录下运行:
Bash
git config user.name "真实姓名或希望显示的名字"
git config user.email "GitHub注册邮箱"提示:
设置后,可以再次运行
git config --list来查看所有有效的配置项。user.email必须是 GitHub 账户中已验证的邮箱地址,这样 GitHub 才能将提交正确归属到自己的账户下。
3. 修复已提交的旧记录 (可选,高级操作)
注意: 修复已经推送到远程仓库的提交是一个高级操作,因为它会修改 Git 历史。
最常用的方法是使用 git commit --amend --reset-author 来修改上一次提交的作者信息。如果要修改更早的提交,可能需要用到 git rebase -i 和 git filter-branch(更复杂)或 git filter-repo(更推荐但需要安装)。
修改最近一次提交:
确保已经用
git config设置了正确的user.name和user.email。执行:
Bash
git commit --amend --reset-author --no-edit--amend: 修改上一次提交。--reset-author: 使用当前配置的user.name和user.email来更新作者信息。--no-edit: 不修改提交信息,保留原来的提交消息。
如果已经推送到远程,需要强制推送:
Bash
git push -f origin <分支名>(例如
git push -f origin master)


