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

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

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

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

F:\Codes\.NETLearning>git log
commit 5e10451f6149808b463c6cdf7bcd08a6f962608e (HEAD -> master)
Author: YeHong <yehong@xxxx.com>
Date:   Sun Nov 4 14:59:10 2018 +0800

    Add Test file

commit a4959ebf3ae7abdab3e98f9eb0e8ef1d6a175b4d
Author: YeHong <yehong@xxxx.com>
Date:   Sun Nov 4 14:58:42 2018 +0800

    Modify README

commit 1bd28ffbd19b6e31b8419d429cdb4aefefa43020 (origin/master, origin/HEAD)
Author: Iron <iron.yehong@outlook.com>
Date:   Sun Nov 4 10:59:06 2018 +0800

    Initial commit

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

git commit --amend

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

修订最近一次提交

git commit --amend --author "Iron <iron.yehong@outlook.com>" --no-edit

输出结果为:

[master 0ace7ed] Add Test file
 Author: Iron <iron.yehong@outlook.com>
 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 <iron.yehong@outlook.com>
# 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 <iron.yehong@outlook.com>" --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 <iron.yehong@outlook.com>" --no-edit
[detached HEAD d262e17] Add Test file
 Author: Iron <iron.yehong@outlook.com>
 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='iron.yehong@outlook.com'"

其中,--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="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi' HEAD

参考资料

<think>好的,用户的问题是关于在IntelliJ IDEA中配置或修改Git提交用户名邮箱。我需要根据提供的引用内容来组织回答。首先,用户提到他们想在IDEA中更改Git提交时使用的信息,所以应该分步骤指导。 根据引用[1],使用Git命令可以直接修改项目的用户名邮箱。但用户可能更希望在IDE中操作,所以需要结合引用[3]提到的IntelliJ IDEA配置Git的步骤。引用[2]说明了如何打开设置,这是关键的第一步。 接下来,我需要确认在IDEA的设置中找到版本控制中的Git部分,然后修改用户信息。可能需要分全局项目级别的配置,因为用户可能希望同项目使用同的信息。同时,提醒用户如果项目有单独的配置会覆盖全局设置,这一点很重要,引用了引用[1]的内容。 另外,还要检查是否配置成功,可能需要通过提交测试或终端命令验证。最后,生成相关问题,比如如何配置Git路径、处理多账号等,这些是基于用户可能遇到的后续问题。</think>在IntelliJ IDEA中配置或修改Git提交用户名邮箱,可以通过以下步骤实现: ### 步骤1:打开设置界面 - **Windows/Linux**:通过菜单栏选择 `File -> Settings` - **macOS**:通过菜单栏选择 `IntelliJ IDEA -> Preferences`[^2] ### 步骤2:定位Git配置 1. 在设置界面左侧导航栏选择 `Version Control -> Git` 2. 确认已正确配置Git可执行文件路径(默认自动检测) ### 步骤3:配置用户信息 #### 全局配置(所有项目生效) ```shell $ git config --global user.name "Your Name" $ git config --global user.email "[email protected]" ``` #### 项目级配置(仅当前项目生效) ```shell $ git config user.name "Project Specific Name" $ git config user.email "[email protected]" [^1] ``` ### 验证配置 1. 通过终端执行命令: ```shell $ git config --global user.name # 查看全局用户名 $ git config user.name # 查看项目级用户名 ``` ### 注意事项 - 项目级配置会覆盖全局配置,若需强制使用全局配置可添加`--global`参数 - 配置变更对后续提交生效,修改历史提交记录 - 多人协作项目建议统一邮箱命名规范
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ironyho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值