(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
参考:https://git-scm.com/docs/git-filter-branch
参考:https://git-scm.com/docs/git-commit
参考:https://www.codenong.com/2919878/
参考:https://scarletsky.github.io/2016/12/29/tilde-and-caret-in-git/

如果你也碰到git/github上提交用户名或者email错误的话,下面介绍的修改方法可能可以帮助到你。
修改commit记录,主要通过使用git filter-branch命令来实现。

1. git filter-branch说明

git filter-branch功能很强大,可以用于对commit记录的修改,修改结果也是比较符合预期的。
当然用的时候也要特别小心,因为该修改并不可逆,如果提交修改后,再想回退就比较困难了。
因为数据回退需要commit历史,还原依赖commit历史;而当前修改的正是commit历史记录,对回退依据的修改是不可回退的。

git filter-branch在官方的声明中标识:
使用该命令,可能会对预期的历史重写造成不明显的破坏,不建议使用。
如果您仍然需要使用git filter branch,请仔细阅读安全(和性能)以了解filter branch的缺陷,然后警惕地尽可能避免其中列出的危险。

语法:

git filter-branch [–setup ] [–subdirectory-filter ]
[–env-filter ] [–tree-filter ]
[–index-filter ] [–parent-filter ]
[–msg-filter ] [–commit-filter ]
[–tag-name-filter ] [–prune-empty]
[–original ] [-d ] [-f | --force]
[–state-branch ] [–] […​]

我们对于提交name/email的修改使用的命令是其中的一项:–env-filter,另外添加-f选项,可选添加的修改范围rev-list
–env-filter

This filter may be used if you only need to modify the environment in which the commit will be performed.
Specifically, you might want to rewrite the author/committer name/email/time environment variables.
The argument is always evaluated in the shell context using the eval command (with the notable exception of the commit filter, for technical reasons).
If any evaluation of returns a non-zero exit status, the whole operation will be aborted.

-f/–force

git filter-branch refuses to start with an existing temporary directory or when there are already refs starting with refs/original/, unless forced.

rev-list options

Arguments for git rev-list.
All positive refs included by these options are rewritten.
You may also specify options such as --all, but you must use – to separate them from the git filter-branch options. Implies Remap to ancestor.
By using git-rev-list[1] arguments, e.g., path limiters, you can limit the set of revisions which get rewritten.
例如:范围使用 HEAD~2…HEAD,只修改最近的二次提交版本

2. env-filter说明与打印信息

env-filter专注于提交名称、提交email、提交日期的重写,包含下面几项:
GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE

windows下可以通过下面命令打印出信息:
// 经尝试linux下,windows下命令可以打印出来,linux下打印不出来
—打印GIT_COMMITTER_NAME

git filter-branch -f --env-filter "echo $GIT_COMMITTER_NAME; "

—打印AUTHOR信息

git filter-branch -f --env-filter "echo $GIT_AUTHOR_NAME, $GIT_AUTHOR_EMAIL, $GIT_AUTHOR_DATE; "

—打印COMMITTER信息

git filter-branch -f --env-filter "echo $GIT_COMMITTER_NAME, $GIT_COMMITTER_EMAIL, $GIT_COMMITTER_DATE; "

3. 通过env-filter无条件改写信息

无条件重写比较简单,但无条件重写意味着不加判断的重写,会把所有涉及项重写;
注意:可能是他人的提交内容也被重写,要慎重使用。
无条件重写命令比较简单,如下所示,注意赋值时没有$取值符号;
样例:
—重写COMMIT-NAME

git filter-branch -f --env-filter "GIT_COMMITTER_NAME=‘DemoName’; "

—重写AUTOR

git filter-branch -f --env-filter "GIT_AUTHOR_NAME=‘AuthName’; GIT_AUTHOR_EMAIL=‘AuthEmail’; "

—重写COMMIT

git filter-branch -f --env-filter “GIT_COMMITTER_NAME=‘CommitName’; GIT_COMMITTER_EMAIL=‘CommitEmail’;”

4. 通过env-filter条件判断改写信息

有条件重写通常是比较可靠一点的,基于条件判定是否重写,通常用起来会比较安全一点;
基于判定,匹配条件时再重写,但是对于这个表达式,linux与windows下并不一致,需要分开来写,另外需要注意表达式中[]前后均需要有空格。
注意:使用前做好备份,可能会当前的refs/heads/master造成损坏的影响,此时一方面是不提交,另一方面是从新clone下载
注意:windows与linux使用不同命令,尝试发现,下面这种条件替换命令,使用’与"差异也会造成命令执行异常

样例:windows下使用命令
—替换COMMIT-NAME

git filter-branch -f --env-filter "if [ $GIT_COMMITTER_NAME == ‘CommitName’ ]; then GIT_COMMITTER_NAME=‘Demo’; fi "

—替换AUTHOR-NAME

git filter-branch -f --env-filter "if [ $GIT_AUTHOR_NAME == ‘AuthName’ ]; then GIT_AUTHOR_NAME=‘Demo’; fi "

—替换COMMIT-EMAIL

git filter-branch -f --env-filter "if [ $GIT_COMMITTER_EMAIL == ‘CommitEmail’ ]; then GIT_COMMITTER_EMAIL=‘Demo’; fi "

—替换AUTHOR-EMAIL

git filter-branch -f --env-filter "if [ $GIT_AUTHOR_EMAIL == ‘AuthEmail’ ]; then GIT_AUTHOR_EMAIL=‘Demo’; fi "

样例:linux下使用命令
—替换COMMIT-NAME

git filter-branch -f --env-filter 'if [ “$GIT_COMMITTER_NAME” = “CommitName” ]; then GIT_COMMITTER_NAME=“Demo”; fi ’

—替换AUTHOR-NAME

git filter-branch -f --env-filter 'if [ “$GIT_AUTHOR_NAME” = “AuthName” ]; then GIT_AUTHOR_NAME=“Demo”; fi ’

—替换COMMIT-EMAIL

git filter-branch -f --env-filter 'if [ “$GIT_COMMITTER_EMAIL” = “CommitEmail” ]; then GIT_COMMITTER_EMAIL=“Demo”; fi ’

—替换AUTHOR-EMAIL

git filter-branch -f --env-filter 'if [ “$GIT_AUTHOR_EMAIL” = “AuthEmail” ]; then GIT_AUTHOR_EMAIL=“Demo”; fi ’

5. 附加说明

修改之后,使用git log查看日志是否已经被改写好了

git log

如果发现出现了异常,那就不要提交修改了,使用git clone重新获取git库,丢弃掉修改内容

git clone xxx.git

如果改写好了,就可以使用git push -f来提交改写了

git push -f

另外修改时,也可以指定分支范围:
例如只修改最后一次提交内容,就可以指定rev-list为

git filter-branch -f --env-filter "GIT_AUTHOR_NAME=‘DemoName’; " HEAD~…HEAD

例如:修改最近三次提交的记录(~的用法参见参考文档中)

git filter-branch -f --env-filter "GIT_AUTHOR_NAME=‘DemoName’; " HEAD~3…HEAD

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

修改git提交commit信息NAME和EMAIL相关推荐

  1. 如何修改已提交commit信息

    如何修改已提交commit信息 1. 修改commit信息 1.1 修改最后一次提交信息 通过git log查看提交历史信息: 输入命令: git commit -m "amend comm ...

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

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

  3. git commit --amend 修改git提交记录用法详解

    有时你提交过代码之后,发现一个地方改错了,你下次提交时不想保留上一次的记录:或者你上一次的commit message的描述有误,这时候你可以使用接下来的这个命令:git commit --amend ...

  4. 解决git提交敏感信息(回退git版本库到某一个commit)

    解决git提交敏感信息(回退git版本库到某一个commit) Fri 07 June 2013 git是一个很好的版本库, 现在很多人用它, 并在github上创建项目, 相信大家都有过将敏感信息提 ...

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

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

  6. git登录报错,git clone无法记住用户名密码,修改git提交用户名

    目录 git登录报错 git无法记住用户名密码 修改git提交用户名 git登录报错 报错如下: 这种问题可能是因为你使用的电脑是之前别人用过的,git的用户信息存储的还是之前登录人的账户密码,所以导 ...

  7. git修改已提交commit的Author信息

    在 git 中可以通过 git commit --amend 来修改最近一个已提交 commit 的 Author 信息,使用如下: git commit --amend --author " ...

  8. git修改已提交的信息

    修改已提交的注释 主要命令: git rebase -i master~1 #最后一次 git rebase -i master~5 #最后五次 git rebase -i HEAD~3 #当前版本的 ...

  9. Git撤销已经推送(push)至远端仓库的提交(commit)信息

    原文链接:https://blog.csdn.net/hanchao5272/article/details/79435730 有关修改提交(git commit)信息的方法可以参考: Git com ...

最新文章

  1. 《C++面向对象高效编程(第2版)》——2.29 泛化关系(is-a)
  2. python字符串写入excel-python-xlwt写入excel详解
  3. stl string的erase方法
  4. 【STM32】FreeRTOS 移植到 STM32F103
  5. 内容创业时代,粉丝已死
  6. 前端页面布局常见问题/已踩过的坑大杂烩
  7. HDU4302(map的用法)
  8. Vue的内容分发slot的使用
  9. 时序数据库技术体系 – InfluxDB 多维查询之倒排索引
  10. COSC 1047 – Winter 2019 – Assignment
  11. bzoj 1500 [NOI 2005] 维修数列
  12. 微服务Spring Cloud Eureka 服务端-基本配置(eureka.server.xxx)
  13. 多线程之间通讯JDK1.5-Lock
  14. python-下拉框处理
  15. 数据库存储过程讲解与实例
  16. 安卓没有删除谷歌服务框架
  17. 产品 电信nb接口调用_【IoT】物联网NB-IoT之电信物联网开放平台对接流程浅析
  18. 亚马逊aws申请ses邮件推送攻略
  19. 记服务器遭遇ssh攻击及应对过程
  20. python数据分析实验报告心得_Python实训周总结

热门文章

  1. [听风]TBC单体插件动作条Bartender4
  2. java的中文乱码转换
  3. 名画52 王希孟《千里江山图》
  4. 大数据培训课程Yarn资源调度器作业提交全过程
  5. 学习笔记 - 5步理解Gradle. How build execution is controlled by gradle tasks?
  6. uni-app线上引入阿里矢量图
  7. VTK进行IGES文件的读取及显示
  8. 如何将SketchUp的模型导入PPT?
  9. 亚马逊云科技语音识别服务Amazon Transcribe在中国区域上线实时流式转录
  10. C 懒虫小鑫 SDUT