10

如何批量修改 Git 提交记录中的作者名称和邮箱

 2 years ago
source link: https://ttys3.dev/post/git-batch-change-commit-author-and-email/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何批量修改 Git 提交记录中的作者名称和邮箱

2020-09-07

:: 荒野無燈

:: Mod 2020-09-07(5321be6)

#Git 

September 7, 2020

适用场景:

已经提交了N个commit才发现用的配置(user.nameuser.email)错了,比如要用个人邮箱的,用成了公司邮箱。 基于隐私考虑,我们需要把公司邮箱和昵称替换掉。

主要是用到 git-filter-repoCALLBACKS功能

参考文档 https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html#CALLBACKS

方法很简单:

#将所有用户名中包含的foo替换成ttys3 (注意,不支持中文)
git filter-repo --name-callback 'return name.replace(b"foo", b"ttys3")'

#将所有commit信息的email中包含的 [email protected] 替换成 [email protected]
git filter-repo --email-callback 'return email.replace(b"[email protected]", b"[email protected]")'
  1. 替换是部分匹配的,因此注意使用尽可能长的子串
  2. 操作会重写所有被匹配到的commit, 因此,如果repo已经push到了远程仓库时,操作要慎重

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK