varHarrie / varharrie.github.io

:blue_book: Personal blog site based on github issues.
https://varharrie.github.io
MIT License
3.66k stars 544 forks source link

批量修改git commit author #16

Open varHarrie opened 7 years ago

varHarrie commented 7 years ago

有这么一个需求,我们在新建项目的时候,忘了修改nameemail,沿用了global中的设置,如果提交了一次commit,可以使用:

git commit –amend –author=‘your_email@example.com’

修改上一次提交的author信息

但是,如果提交过不止一次,就不能使用这个方法了。下面是一个批量修改的办法:

#!/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

此方法出自github help

这里有关于这个问题的更多讨论