首页 文章

如何更改一个特定提交的提交作者?

提问于
浏览
1528

我想更改历史记录中某个特定提交的作者 . 这不是最后一次提交 .

我知道这个问题 - How do I change the author of a commit in git?

但我正在考虑一些事情,我通过哈希或短哈希来识别提交 .

9 回答

  • 11

    您链接的问题中的答案是很好的答案并涵盖您的情况(另一个问题是更一般的,因为它涉及重写多个提交) .

    作为尝试 git filter-branch 的借口,我编写了一个脚本来重写给定提交的作者姓名和/或作者电子邮件:

    #!/bin/sh
    
    #
    # Change the author name and/or email of a single commit.
    #
    # change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
    #
    #     If -f is supplied it is passed to "git filter-branch".
    #
    #     If <branch-to-rewrite> is not provided or is empty HEAD will be used.
    #     Use "--all" or a space separated list (e.g. "master next") to rewrite
    #     multiple branches.
    #
    #     If <new-name> (or <new-email>) is not provided or is empty, the normal
    #     user.name (user.email) Git configuration value will be used.
    #
    
    force=''
    if test "x$1" = "x-f"; then
        force='-f'
        shift
    fi
    
    die() {
        printf '%s\n' "$@"
        exit 128
    }
    targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
    br="${2:-HEAD}"
    
    TARG_COMMIT="$targ"
    TARG_NAME="${3-}"
    TARG_EMAIL="${4-}"
    export TARG_COMMIT TARG_NAME TARG_EMAIL
    
    filt='
    
        if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
            if test -n "$TARG_EMAIL"; then
                GIT_AUTHOR_EMAIL="$TARG_EMAIL"
                export GIT_AUTHOR_EMAIL
            else
                unset GIT_AUTHOR_EMAIL
            fi
            if test -n "$TARG_NAME"; then
                GIT_AUTHOR_NAME="$TARG_NAME"
                export GIT_AUTHOR_NAME
            else
                unset GIT_AUTHOR_NAME
            fi
        fi
    
    '
    
    git filter-branch $force --env-filter "$filt" -- $br
    
  • 6
    • 将您的电子邮件重置为全局配置:

    git config --global user.email example@email.com

    • 现在重置您的提交作者,无需编辑:

    git commit --amend --reset-author --no-edit

  • 12

    承诺之前:

    enter image description here

    要修复所有提交的作者,您可以从@ Amber的答案中应用命令:

    git commit --amend --author="Author Name <email@address.com>"
    

    或者重复使用您的姓名和电子邮件,您可以写:

    git commit --amend --author=Eugen
    

    命令后提交:

    enter image description here

    例如,要从 4025621 开始更改所有内容:

    enter image description here

    你必须运行:

    git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621
    

    注意:要包含包含名称和电子邮件地址等空格的作者,作者必须被转义引号括起来 . 例如:

    git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621
    

    或者将此别名添加到 ~/.gitconfig 中:

    [alias]
        reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --
    

    然后运行:

    git reauthor 4025621 Eugen
    
  • 47

    交互式rebase关闭历史记录中早于您需要修改的提交( git rebase -i <earliercommit> ) . 在要重新提交的提交列表中,将文本从 pick 更改为要修改的哈希旁边的 edit . 然后,当git提示您更改提交时,请使用以下命令:

    git commit --amend --author="Author Name <email@address.com>"
    

    例如,如果您的提交历史记录为 A-B-C-D-E-FFHEAD ,并且您想要更改 CD 的作者,那么您会......

    • 指定 git rebase -i Bhere is an example of what you will see after executing the git rebase -i B command

    • 如果您需要编辑 A ,请使用 git rebase -i --root

    • CD 的行从 pick 更改为 edit

    • 一旦rebase开始,它将首先暂停 C

    • 你会 git commit --amend --author="Author Name <email@address.com>"

    • 然后 git rebase --continue

    • 会再次暂停 D

    • 然后你会再次 git commit --amend --author="Author Name <email@address.com>"

    • git rebase --continue

    • rebase将完成 .

    • 使用 git push -f 使用更新的提交更新您的来源 .

  • 74

    这个问题的accepted answer非常巧妙地使用了交互式rebase,但不幸的是,如果我们试图改变以前的作者的提交是在一个随后被合并的分支上,那么它就会出现冲突 . 更一般地说,它不起作用处理凌乱的历史 .

    由于我担心运行依赖于设置和取消设置环境变量的脚本来重写git历史记录,我正在编写一个基于this post的新答案,它类似于this answer但更完整 .

    与链接的答案不同,以下内容经过测试和运行 . 为了清楚地说明 03f482d6 是我们正在尝试替换的作者的提交,并且 42627abe 是新作者的提交 .

    • 签出我们试图修改的提交 .
    git checkout 03f482d6
    
    • 让作者改变 .
    git commit --amend --author "New Author Name <New Author Email>"
    

    现在我们有一个新的提交,哈希假定为 42627abe .

    • 签出原始分支 .

    • 用本地新提交替换旧提交 .

    git replace 03f482d6 42627abe
    
    • 根据替换重写所有未来的提交 .
    git filter-branch -- --all
    
    • 取消更换清洁度 .
    git replace -d 03f482d6
    
    • 推送新历史记录(如果以下操作失败,则仅使用--force,并且仅在使用 git log 和/或 git diff 进行完整性检查后) .
    git push --force-with-lease
    

    而不是4-6你可以改变到新的提交:

    git rebase -i 42627abe
    
  • 146

    如果您使用的是集中式存储库,那么Amber's answer还有一个额外的步骤:

    git push -f 强制更新中央存储库 .

    要小心,没有很多人在同一个分支上工作,因为它会破坏一致性 .

  • 358

    Github文档包含a script that replaces the committer info for all commits in a branch .

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

    在做 git rebase -i 时,文档中有一个有趣的内容:

    如果要将两个或多个提交折叠成一个,请使用“squash”或“fixup”替换第二个和后续提交的“pick”命令 . 如果提交具有不同的作者,则折叠的提交将归因于第一次提交的作者 . 折叠提交的建议提交消息是第一次提交的提交消息和具有“squash”命令的提交消息的串联,但是省略了使用“fixup”命令提交的提交消息 .

    • 如果你有 A-B-C-D-E-F 的历史,

    • 并且您想要更改提交 BD (= 2次提交),

    然后你可以这样做:

    • git config user.name "Correct new name"

    • git config user.email "correct@new.email"

    • 创建空提交(每次提交一次):

    • 你需要一条消息用于rebase目的

    • git commit --allow-empty -m "empty"

    • 启动rebase操作

    • git rebase -i B^

    • B^ 选择 B 的父级 .

    • 你想要将每个提交一个空提交 before 修改

    • 您需要将 pick 更改为 squash .

    git rebase -i B^ 将为您提供的示例:

    pick sha-commit-B some message
    pick sha-commit-C some message
    pick sha-commit-D some message
    pick sha-commit-E some message
    pick sha-commit-F some message
    # pick sha-commit-empty1 empty
    # pick sha-commit-empty2 empty
    

    改为:

    # change commit B's author
    pick sha-commit-empty1 empty
    squash sha-commit-B some message
    # leave commit C alone
    pick sha-commit-C some message
    # change commit D's author
    pick sha-commit-empty2 empty
    squash sha-commit-D some message
    # leave commit E-F alone
    pick sha-commit-E some message
    pick sha-commit-F some message
    

    它会提示您编辑消息:

    # This is a combination of 2 commits.
    # The first commit's message is:
    
    empty
    
    # This is the 2nd commit message:
    
    ...some useful commit message there...
    

    你可以删除前几行 .

  • 67

    您可以使用以下命令更改上次提交的作者 .

    git commit --amend --author="Author Name <email@address.com>"

    但是,如果要更改多个提交作者名称,则有点棘手 . 您需要启动交互式rebase,然后将提交标记为编辑,然后逐个修改它们并完成 .

    使用 git rebase -i 开始变基 . 它会告诉你这样的事情 .

    https://monosnap.com/file/G7sdn66k7JWpT91uiOUAQWMhPrMQVT.png

    对于要更改作者姓名的提交,请将 pick 关键字更改为 edit .

    https://monosnap.com/file/dsq0AfopQMVskBNknz6GZZwlWGVwWU.png

    然后关闭编辑器 . 对于初学者,点击 Escape 然后输入 :wq 并点击 Enter .

    然后你会看到你的终端没有发生任何事情 . 实际上,你正处于一个互动的rebase中间 . 现在它使用上面的命令's time to amend your commit'作者姓名 . 它将再次打开编辑器 . 退出并继续使用 git rebase --continue 进行rebase . 对要编辑的提交计数重复相同的操作 . 收到 No rebase in progress? 消息后,您可以确保交互式rebase已完成 .

相关问题