变更记录

序号 录入时间 录入人 备注
1 2017-08-29 Alfred Jiang -

方案名称

Git - 通过 Shell 脚本批量修改历史递交记录中的用户名和邮箱

关键字

Git \ Shell 脚本 \ 批量修改历史递交记录

需求场景

参考链接

  1. Segmentfault - Git批量修改历史commit中的user.name 和user.email
  2. GitHub - Changing author info

详细内容

1. 在仓库根目录(.git 文件所在目录)编写以下脚本并执行,替换 your-old-email@example.comYour Correct Nameyour-correct-email@example.com 为你需要修改的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh

git filter-branch --env-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
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

2. 执行以下命令将修改后的仓库历史推到远程

1
git push --force --tags origin 'refs/heads/*'

效果图

(无)

备注

(无)