Git 修改历史提交中的用户名和邮箱

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Iron_Ye/article/details/83747266

Git 修改历史提交中的用户名和邮箱

最近几次贡献开源代码总是遇到一个问题,我将 GitHub 上的项目 clone 到本地,完成编码后直接 commit(提交) 。提交后才发现没有使用 git config 来为项目配置私人用户名和邮箱,因此提交中携带的是全局配置中的公司账户信息。为了避免回滚代码,只有寻找办法来修订提交中的信息。本文将要介绍的便是这类修改历史提交中的用户名和邮箱的方法,这些方法也同样适用于修改历史提交中的其它信息,比如 message 等。

首先,通过 git log 命令查看一下最近提交:

F:\Codes\.NETLearning>git log
commit 5e10451f6149808b463c6cdf7bcd08a6f962608e (HEAD -> master)
Author: YeHong <[email protected]>
Date:   Sun Nov 4 14:59:10 2018 +0800

    Add Test file

commit a4959ebf3ae7abdab3e98f9eb0e8ef1d6a175b4d
Author: YeHong <[email protected]>
Date:   Sun Nov 4 14:58:42 2018 +0800

    Modify README

commit 1bd28ffbd19b6e31b8419d429cdb4aefefa43020 (origin/master, origin/HEAD)
Author: Iron <[email protected]>
Date:   Sun Nov 4 10:59:06 2018 +0800

    Initial commit

其中,最近的两次提交中携带的是我在公司仓库中的用户名和密码,因此需要将其中的 YeHong <[email protected]> 修改为 Iron <[email protected]>,本文将介绍两种修正方法。

git commit --amend

git commit --amend 命令用于对提交信息进行修订。

修订最近一次提交

git commit --amend --author "Iron <[email protected]>" --no-edit

输出结果为:

[master 0ace7ed] Add Test file
 Author: Iron <[email protected]>
 Date: Sun Nov 4 14:59:10 2018 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Test.txt

其中,--no-edit 参数可以避免弹出编辑器。如果不带此参数,则会弹出 vim 编辑器让你进一步编辑:

Add Test file

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author:    Iron <[email protected]>
# Date:      Sun Nov 4 14:59:10 2018 +0800
#
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#       new file:   Test.txt
#

编辑好后,输入 :wq 保存并退出即可。同样,git commit --amend 也可用于修改提交中的其它信息,例如:

git commit --amend --message "Add Test.txt file" --no-edit

修正多个提交

可使用 git rebase -i 来修改最近的多次提交,其后可以跟上最近的提交次数 HEAD~n,或者跟上某次提交的 SHA-1 值。

git rebase -i HEAD~2

或者

git rebase -i 1bd28ffbd19

如上两条命令,前者指的是对最近的两次提交进行交互式变基操作,后者指的是对 SHA-1 值为 1bd28ffbd19 的那次提交(不包括)到最新提交(包括)之间的所有提交进行交互式变基。如上命令会弹出编辑器:

pick a4959eb Modify README
pick 5e10451 Add Test file

# Rebase 1bd28ff..5e10451 onto 1bd28ff (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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

将需要进行修订的提交前的 pick 改为为 edit

edit a4959eb Modify README
edit 5e10451 Add Test file

然后输入 :wq 保存并退出,输出如下提示:

Stopped at a4959eb...  Modify README
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

当前已经停留在 a4959eb... 这次提交处,接下来可以使用前面的 git commit --amend 来进行修订,然后使用 git rebase --continue 跳转到下一个提交:

F:\Codes\.NETLearning>git commit --amend --author "Iron <[email protected]>" --no-edit
[detached HEAD 24d5789] Modify README
 Date: Sun Nov 4 14:58:42 2018 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)

F:\Codes\.NETLearning>git rebase --continue
Stopped at 5e10451...  Add Test file
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

F:\Codes\.NETLearning>git commit --amend --author "Iron <[email protected]>" --no-edit
[detached HEAD d262e17] Add Test file
 Author: Iron <[email protected]>
 Date: Sun Nov 4 14:59:10 2018 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Test.txt

git filter-branch

git filter-branch 允许使用一个单一命令来大范围地更改历史。

git filter-branch --commit-filter "GIT_AUTHOR_NAME='Iron'; GIT_AUTHOR_EMAIL='[email protected]'"

其中,--commit-filter 指的是提交过滤器,即对提交内容进行修改。同级的参数还有 --tree-filter--env-filter 等。如上命令的输出结果为:

Rewrite 5e10451f6149808b463c6cdf7bcd08a6f962608e (3/3) (7 seconds passed, remaining 0 predicted)
Ref 'HEAD' was rewritten

即修订了每次提交中的用户名和邮箱,如果只想对符合条件的提交进行修订,则可参考如下脚本:

#!/bin/sh

git filter-branch --commit-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi' HEAD

参考资料

猜你喜欢

转载自blog.csdn.net/Iron_Ye/article/details/83747266
今日推荐