如何修改单个提交的作者信息
改最近一次提交的作者:git commit --amend --author="名字 <邮箱>"。改历史中间的提交:git rebase -i <提交>^ 标记 edit,再 amend --author。批量改用 git filter-repo --email-callback。改完需强推。
最近一次提交用 git commit --amend --author="张三 <zhangsan@example.com>" 直接改。 历史中间的提交走交互式 rebase:标记 edit → amend → continue。注意改作者会改变提交哈希,已推送的分支需要强推。
改最近一次提交
git commit --amend --author="Zhang San <zhangsan@example.com>" --no-edit
改历史中间的提交
git rebase -i a1b2c3d^ # 目标提交的父提交
# 编辑器里把目标行 pick 改成 edit,保存退出
git commit --amend --author="Zhang San <zhangsan@example.com>" --no-edit
git rebase --continue
批量修改(如把公司邮箱全换成个人邮箱)
git filter-repo --email-callback '
return email.replace(b"@old-company.com", b"@gmail.com")
'
filter-repo 会重写整个历史,适合"整个仓库换作者身份"的场景。
区分 author 与 committer
--author改的是作者(写代码的人);- 提交者(committer,执行提交的人)通过环境变量改:
GIT_COMMITTER_NAME="Zhang San" GIT_COMMITTER_EMAIL="z@example.com" git commit --amend --no-edit
常见问题(FAQ)
Q:改完 push 被拒绝?
amend 改写了历史,需要 git push --force-with-lease。共享分支先和团队打招呼。
Q:GitHub 上提交不显示我的头像/不计入贡献?
提交邮箱与 GitHub 账号绑定的邮箱不一致。改对邮箱(或把该邮箱加到 GitHub 账号的 Emails 里),历史提交会自动关联。
Q:怎样避免今后再提交错身份?
按项目配置身份:git config user.email "work@company.com"(不加 --global,只作用于当前仓库);或用 conditional includes 按目录自动切换身份。