Niffler

X2.06 15352103-Git变更作者信息

Posted By 15352103

问题

最近突然发现自己在github上的commit和push没有记录在contributions中,例如在仓库的commits记录中没有显示我的头像:

not show avatar

在网上查了一下,原来是git设置的email和github设置的email不匹配。完全不记得什么时候改过git的email,感觉要被自己蠢哭了……

解决方法

还好github官网给出了详细的解决方法,参考Changing author info

更改提交记录中已经记录的名字和邮箱地址,必须重写仓库的整个历史。此操作对仓库的历史具有破坏性,并不推荐。github帮助提供了一个脚本,此脚本将变更在作者字段中具有旧邮箱地址的任何提交记录,从而使用正确的名字和邮箱地址。运行完脚本后,任何fork或clone此仓库的人必须重新获取已经重写的历史,并将任何本地变更rebase到历史中。

  1. 创建仓库的bare克隆
git clone --bare https://github.com/user/repo.git
cd repo.git

clone cd

  1. 更改OLD_EMAIL, CORRECT_NAME, CORRECT_EMAIL三个变量并运行脚本
#!/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

script

  1. 将正确的历史push到github
git push --force --tags origin 'refs/heads/*'

push

  1. 删除临时仓库
cd ..
rm -rf repo.git

remove

打开github,发现问题已解决

show avatar