本地有多个 git 账户,有时候提交没注意导致出现多个作者。如何修复这个问题?


1.批量修改 sh

这是我基于 github 官方文档修改的 sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#第一步---------------------------
#!/bin/sh

git filter-branch -f --env-filter '

OLD_EMAILs=("xx1@hotmail.com" "xx2@xx.com")
OLD_Names=("name1" "name2" "name3")

CORRECT_NAME="正确的名字"
CORRECT_EMAIL="正确的邮箱"

for old_Email in ${OLD_EMAILs[*]}; do
    if [ "$GIT_COMMITTER_EMAIL" = "$old_Email" ]; then
        echo 替换COMMITTER Email "$GIT_AUTHOR_NAME" 为 "$CORRECT_NAME" "$CORRECT_EMAIL"
        export GIT_COMMITTER_NAME="$CORRECT_NAME"
        export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$old_Email" ]; then
        echo 替换AUTHOR Email "$GIT_AUTHOR_NAME" 为 "$CORRECT_NAME" "$CORRECT_EMAIL"
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
done

for name in ${OLD_Names[*]}; do
    if [ "$GIT_AUTHOR_NAME" = ${name} ]; then
        echo 替换Name "$GIT_AUTHOR_NAME" 为 "$CORRECT_NAME" "$CORRECT_EMAIL"
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
done

' --tag-name-filter cat -- --branches --tags

#第二步-------------------------------
git push --force --tags origin 'refs/heads/*'

2.使用方法

  • 全部修改上面的 sh
1
2
3
4
5
OLD_EMAILs=("xx1@hotmail.com" "xx2@xx.com")
OLD_Names=("name1" "name2" "name3")

CORRECT_NAME="正确的名字"
CORRECT_EMAIL="正确的邮箱"
  • 在需要修改的工程,打开命令行 bash 或者 powershell 等。
  • 粘贴所有代码
  • 执行,完事