如何修改 Git 中的多个提交以更改作者

问题描述 投票:0回答:8

我在 Git 中进行了一系列提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推送到我的存储库,那么在我这样做之前如何更正这些提交(只有 master 分支上的 3 个最新提交)?

我一直在看

git reset
git commit -C <id> --reset-author
,但我不认为我在正确的轨道上

git git-commit git-rewrite-history
8个回答
322
投票

警告:现在弃用支持filter-repo

Rebase/amend 似乎效率低下,当你拥有触手可及的 filter-branch 的力量时:

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

(为清楚起见,跨行拆分,但不是必需的)

完成后一定要检查结果,以确保您没有更改任何您不想更改的内容!


272
投票

与 exec 结合使用时,交互式变基方法非常好。您可以针对特定提交或 rebase 中的所有提交运行任何 shell 命令。

首先设置你的 git 作者设置

git config --global user.name "John Doe"
git config --global user.email [email protected]

然后为所有提交重置作者给定的

BASE_SHA

git rebase -i BASE_SHA -x \
  "git commit --amend --author 'John Doe <[email protected]>' -CHEAD"

这将弹出您的编辑器以确认更改。您在这里需要做的就是保存并退出,它将遍历每次提交并运行 -x 标志中指定的命令。


150
投票

仅更改最后一次提交的作者:

git commit --amend --author 'Author Name <[email protected]>' --no-edit

假设您只想更改最后 N 次提交的作者:

git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"

注意事项

  • --no-edit
    标志确保
    git commit --amend
    不要求额外确认
  • 当您使用
    git rebase -i
    时,您可以手动选择更改作者的提交,

您编辑的文件将如下所示:

pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <[email protected]>' --no-edit
pick dc18f70 bugfix

40
投票

这里投票最高的答案现在已经过时了。 Git 在使用 git filter-branch 时显示这个可怕的警告 -

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites. Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.

filter-repo
(还)不是 git 的一部分,需要单独安装。

# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
    
# Install using pip
pip install git-filter-repo    
    

要替换仅以前提交中的电子邮件,运行这样的命令-

git filter-repo --email-callback '
    return email if email != b"incorrect@email" else b"correct@email"
' 

要替换两者,email 和 author name 在以前的提交中运行这样的命令 -

git filter-repo --commit-callback '
    if commit.author_email == b"incorrect@email":
        commit.author_email = b"correct@email" 
        commit.author_name = b"Correct Name"
        commit.committer_email = b"correct@email" 
        commit.committer_name = b"Correct Name"
' 

确保在终端中粘贴命令时有缩进。回调使用 python 语法,因此缩进很重要。

docs 中阅读更多关于 filter-repo 回调的信息。


13
投票

GitHub 为此目的记录了此方法(尽管 GitHub 已将其删除)。步骤是:

  1. 打开终端并制作一个 bare 克隆你的 repo
git clone --bare https://github.com/user/repo.git
cd repo
  1. 编辑以下脚本(替换
    OLD_EMAIL
    CORRECT_EMAIL
    CORRECT_NAME
#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
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. 将脚本复制/粘贴到您的终端中,然后按回车键运行它。
  2. 使用
    git push --force --tags origin 'refs/heads/*'
    推送您的更改,您就完成了!

2
投票

我相信你要找的是

git rebase --interactive

它允许您重置到特定的提交,然后抛出更改添加或分组提交的历史

这里有解释https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git -rebase-interactive


2
投票

如果你正在寻找剧本,这个对我来说很方便。

  1. GitHub 下载脚本并将其保存到易于访问的位置。

  2. 更改脚本文件的权限以允许其执行:

    chmod +x changeauthor.sh

  3. 使用不正确的提交历史导航到存储库

    cd path/to/repo

  4. 运行脚本(带或不带标志)

    ../path/to/changeauthor.sh --old-email [email protected] \
        --new-email [email protected] --new-name "Kaka Ruto" --remote origin
    

小心,因为这将重写当前目录存储库中的所有历史记录!好消息是脚本会为您提供有关您将要执行的操作的警告和信息

在这里阅读更多 https://www.adamdehaven.com/blog/update-commit-history-author-information-for-git-repository/


0
投票

this answer中所建议

git-filter-repo
是该任务的首选。

然而,只需更改作者姓名和/或电子邮件,就可以使用

--mailmap
--use-mailmap
而不是回调。

需要根据格式创建mailmap文件(见git-filter-repo/docsgit-scm.com/docs

然后简单地运行

git filter-repo --mailmap .mailmap
© www.soinside.com 2019 - 2024. All rights reserved.