【转载】修改 Github commit 的作者信息

配置方法:

git config --global user.email "youremail@google.com"
git config --global user.name "your name"

git config --local user.email "youremail@google.com"
git config --local user.name "your name"

但是补救措施只对以后的 commit 起效。 
如果想修改之前的作者信息,可以通过脚本重写历史信息: 
1. 创建一个你的 repo 的全新裸 clone (repo.git 替换为你的项目,下同)

git clone --bare https://github.com/user/repo.git
cd repo.git

2.复制粘贴脚本,并根据你的信息修改以下变量:

OLD_EMAIL # 历史记录中的邮箱
CORRECT_NAME # 新的用户名
CORRECT_EMAIL # 新的邮箱

3.脚本

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

4.按 Enter 执行脚本。

5.查看新 Git 历史有没有错误。

6.把正确历史 push 到 Github:(push 有困难时记得修改 DNS 或者搭梯子)

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

7.清除临时 clone

cd ..
rm -rf repo.git

 原文链接:https://www.jianshu.com/p/b6add8187c06

posted @ 2018-04-02 20:30  AbbyChen  阅读(1437)  评论(0编辑  收藏  举报