GitHub 修改作者信息

Author Avatar
James Yeung 9月 11, 2020
  • 在其它设备中阅读本文章

当提交项目前,没有把本地仓库的用户名和邮箱设置成 Github 账号的话,提交后的 commit 就不会被识别为 GitHub 用户,所以这里记一下如何修改这些记录。

首先,需要把本地仓库的用户名和邮箱设置成跟 Github 账号一致的。

然后,执行 rebase 命令,一个个地修改 commit。例如我要修改最后 5 个 commit,则执行命令:

git rebase -i HEAD~5

然后,在命令行打开了一个文件,是用于设置接下来对这 5 个 commit 需要做的操作

pick d8ca4ea initial project
pick 4057007 refactor api service
pick a875b98 feat: add http proxy
pick e9d6fe8 feat: setup server prerendering
pick 8cfe3e4 feat: add identity configuration

# Rebase 54a0c1c..8cfe3e4 onto e9d6fe8 (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~

这里,把前几行的 commit 前面的 pick 改为 edit,即:

edit d8ca4ea initial project
edit 4057007 refactor api service
edit a875b98 feat: add http proxy
edit e9d6fe8 feat: setup server prerendering
edit 8cfe3e4 feat: add identity configuration

然后按 Esc 退出,输入 :qw 保存并退出。此时就进入了 rebase 状态。

You can amend the commit now,with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

意思就是,运行 git commit --amend 修改,修改完后运行 git rebase --continue 命令继续下一条,知道修改完所有 commit。

由于我们是要修改作者,所以要改为运行 git commit --amend --author "James Yeung <[email protected]>" --no-edit

然后再运行 git rebase --continue

重复 5 次,就完成了对作者的修改。最后,force push 到 GitHub 上就可以完成历史修改了。

本文原创授权为:署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 协议普通文本 | 协议法律文本
本文链接:https://yangshunjie.com/modify-the-author-of-git-commit.html