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 +0800Add Test filecommit a4959ebf3ae7abdab3e98f9eb0e8ef1d6a175b4d
Author: YeHong <yehong@xxxx.com>
Date:   Sun Nov 4 14:58:42 2018 +0800Modify READMEcommit 1bd28ffbd19b6e31b8419d429cdb4aefefa43020 (origin/master, origin/HEAD)
Author: Iron <iron.yehong@outlook.com>
Date:   Sun Nov 4 10:59:06 2018 +0800Initial 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 fileAuthor: Iron <iron.yehong@outlook.com>Date: Sun Nov 4 14:59:10 2018 +08001 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, withgit commit --amendOnce you are satisfied with your changes, rungit 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 READMEDate: Sun Nov 4 14:58:42 2018 +08001 file changed, 1 insertion(+), 1 deletion(-)F:\Codes\.NETLearning>git rebase --continue
Stopped at 5e10451...  Add Test file
You can amend the commit now, withgit commit --amendOnce you are satisfied with your changes, rungit rebase --continueF:\Codes\.NETLearning>git commit --amend --author "Iron <iron.yehong@outlook.com>" --no-edit
[detached HEAD d262e17] Add Test fileAuthor: Iron <iron.yehong@outlook.com>Date: Sun Nov 4 14:59:10 2018 +08001 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/shgit 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" ]
thenexport GIT_COMMITTER_NAME="$CORRECT_NAME"export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi' HEAD

参考资料

  • Changing a commit message
  • git-filter-branch - Rewrite branches
  • How to change the author and committer name and e-mail of multiple commits in Git?
  • Git-工具-重写历史

Git 修改历史提交中的用户名和邮箱相关推荐

  1. git 修改历史提交信息

    当你不小心,写错了提交的注视/信息,该如何处理呢.理论上,SCM是不应该修改历史的信息的,提交的注释也是.    不过在git中,其commit提供了一个--amend参数,可以修改最后一次提交的信息 ...

  2. 浅谈git rebase命令 -- git修改历史提交信息

    浅谈git rebase命令 如果你只想修改git log命令打印到终端上的最后一个提交信息,那么使用git commit --amend命令直接修改就好. 如果你还想合并最近的某几个提交为一个提交, ...

  3. Git系列之修改历史提交信息

    文章の目录 1.查看 git 提交记录 2.修改最近两个或者两次上的commit信息 3.扩展:修改上一次git commit 提交的信息 参考 写在最后 1.查看 git 提交记录 git log ...

  4. 修改git已提交的commit中提交者的用户名和邮箱

    如下图中,将已经提交的commit中,用户名jincheng-demo,jincheng-demo01,以及邮箱jincheng@test.com, 修改为正确的用户名:jincheng,正确的邮箱: ...

  5. 修改 Git 已经提交记录的 用户名 和邮箱

    修改 Git 已经提交记录的 用户名 和邮箱 有关 Git 和版本控制的常见问题. 如何更改提交的作者姓名/电子邮件? 在我们进入解决方案之前,让我们找出您到底想要完成什么: 在提交之前更改作者信息 ...

  6. 基于Git rebase修改历史提交信息

    说明 关于为什么要修改历史提交的作者信息和提交信息,我就不多说了,也许是项目规范要求,也许是自己强迫症,总之是有这样的需求. 开始之前先说明一下: 本文介绍的方法只适用于本地仓库,提交到远程以后,我没 ...

  7. 【mac如何修改git本地提交代码的用户名和邮箱】

    mac如何修改git本地提交代码的用户名和邮箱 入职新公司,推完代码发现推送人不是自己,因为电脑里还是前同事的用户信息,所以我们需要在本地客户端重新设置一下. 一.打开mac的终端,查看本地配置 # ...

  8. Git 修改已提交 commit 的信息

    背景 由于 Github 和公司 Git 使用账号不一样,偶尔没注意,提交出错后就需要修改 commit 信息. 修改最后一次提交 commit 的信息 # 修改最近提交的 commit 信息 $ g ...

  9. Git 修改前面提交commit的名字

    Git 修改前面提交commit的名字 1.在命令行输入gitk调出git界面 我现在想做的是把[feat][4G]添加4Gslm的驱动支持改成[feat][4G]添加4Gslm驱动支持. 1.首先回 ...

  10. git idea 修改提交信息_idea中修改git提交代码的用户名和邮箱地址

    Idea中使用git 命令窗口 依次打开idea->file->settings->tools->Terminal 在shell path 选择git安装目录bin中的bash ...

最新文章

  1. 经验分享 | Burpsuite抓取非HTTP流量
  2. 扫盲文章:AMF,RTMP,RTMPT,RTMPS
  3. Java中将String格式的标准时间字符串转换为Date格式的方法
  4. 深度学习(33)随机梯度下降十一: TensorBoard可视化
  5. .NET Core Blazor 1-Blazor项目文件分析
  6. Maven 入门——认识Maven结构
  7. [Threejs]环境光与HDR贴图
  8. 自媒体视频剪辑12大技巧分享
  9. Android Studio 实现播放本地/网络视频
  10. 微信自动回复(新年快乐)
  11. Mybatis 注解开发
  12. 从原理到CMOS图像传感器玩家,一文读懂手机摄像头所有猫腻
  13. 水稻CBL家族蛋白质查找
  14. 问题十四:怎么可视化球的法向量
  15. 史上最全开启windows7(win7)虚拟wifi教程(上)
  16. 使用iterm2查看日志时屏幕持续滚动将老内容冲掉的设置
  17. 突发!活力花借款人被扫黑办传唤取证,与京东数科合作紧密
  18. python金融分析小知识(19)——NLP初探之LDA话题建模
  19. cs224w(图机器学习)2021冬季课程学习笔记4 Link Analysis: PageRank (Graph as Matrix)
  20. 超级账本Fabric中的权限管理和策略

热门文章

  1. 危害移动数据安全的风险有哪些?
  2. UE4_Shader Compile 停在某一帧或者新建材质出现卡死
  3. 微信网页授权失败原因总结
  4. .NET面试宝典130道经典面试真题及答案
  5. Windows8[Web应用程序项目***已配置为使用IIS。无法访问IIS元数据库,您没有足够的特权访问计算机上的IIS网站]
  6. 高压油管matlab,高压油管的压力及流量控制
  7. 服务器虚拟机怎么安装win7系统教程,虚拟机怎么安装系统?VMware虚拟机安装Win7和win10图文详细教程...
  8. 邮编查询经纬度_行政区划省市区邮编区号拼音经纬度全面标准数据库 每月更新...
  9. dell系统重装后无法进入系统_DELL电脑重装系统后出现No bootable devices --strike如何解决...
  10. python 实现线程安全的单例模式