修改Git commit 的author

公司的Gerrit管理机制不允许非注册的author提交的commit被push到repository,于是需要修改git commit里中非注册的author为注册的author。

GitHub已经有类似的应用场景,所以有现成的脚本使用:

https://help.github.com/articles/changing-author-info


摘录如下:

If you need to modify the author info in your repository's history, you can do so with this script.

Warning: This action is destructive to your repository's history. It's best to do this on a clone, just in case. Also beware that this should not be performed on a repository that has been shared with others. Use at your own risk.

#!/bin/sh
 
git filter-branch --env-filter '
 
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
 
if [ "$GIT_COMMITTER_EMAIL" = "your@email.to.match" ]
then
    cn="Your New Committer Name"
    cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "your@email.to.match" ]
then
    an="Your New Author Name"
    am="Your New Author Email"
fi
 
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'