修改GIT已提交的用户名和邮箱

修改GIT已提交的用户名和邮箱

原文:https://help.github.com/en/github/using-git/changing-author-info

说明

要更改在现有提交中记录的名称和/或电子邮件地址,必须重写Git存储库的整个历史。

警告:此操作会破坏存储库的历史记录。如果您正在与其他人协作创建存储库,则重写已发布的历史记录被认为是不好的做法。你应该只在紧急情况下这样做。

使用脚本更改存储库的Git历史记录

使用下面的脚本,更改已提交数据的author或committer。

操作步骤:

  1. 打开Linux Terminal(终端),如果是WIndow系统打开 Git Bash。
  2. 创建一个你的 repo 的全新裸 clone (repo.git 替换为实际项目)
git clone --bare https://github.com/user/repo.git
cd repo.git
  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
  1. 按 Enter 执行脚本。
  2. 查看新 Git 历史有没有错误。
  3. 把正确历史 push 到 Github :
git push --force --tags origin 'refs/heads/*'
  1. 清理临时clone:
cd ..
rm -rf repo.git
原文地址:https://www.cnblogs.com/relucent/p/11916865.html