Change commit author at one specific commit

I want to change the author of one specific commit in the history. It's not the last commit.

I know about this question - How do I change the author of a commit in git?

But I am thinking about something, where I identify the commit by hash or short-hash.


Interactive rebase off of a point earlier in the history than the commit you need to modify ( git rebase -i <earliercommit> ). In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:

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

For example, if your commit history is ABCDEF with F as HEAD , and you want to change the author of C and D , then you would...

  • Specify git rebase -i B (here is an example of what you will see after executing the git rebase -i B command)
  • if you need to edit A , use git rebase -i --root
  • change the lines for both C and D from pick to edit
  • Once the rebase started, it would first pause at C
  • You would git commit --amend --author="Author Name <email@address.com>"
  • Then git rebase --continue
  • It would pause again at D
  • Then you would git commit --amend --author="Author Name <email@address.com>" again
  • git rebase --continue
  • The rebase would complete.

  • The accepted answer to this question is a wonderfully clever use of interactive rebase, but it unfortunately exhibits conflicts if the commit we are trying to change the author of used to be on a branch which was subsequently merged in. More generally, it does not work when handling messy histories.

    Since I am apprehensive about running scripts which depend on setting and unsetting environment variables to rewrite git history, I am writing a new answer based on this post which is similar to this answer but is more complete.

    The following is tested and working, unlike the linked answer. Assume for clarity of exposition that 03f482d6 is the commit whose author we are trying to replace, and 42627abe is the commit with the new author.

  • Checkout the commit we are trying to modify.

    git checkout 03f482d6
    
  • Make the author change.

    git commit --amend --author "New Author Name <New Author Email>"
    

    and then checkout the original branch.

  • Replace the old commit with the new one locally.

    git replace 03f482d6 42627abe
    
  • Rewrite all future commits based on the replacement.

    git filter-branch -- --all
    
  • Remove the replacement for cleanliness.

    git replace -d 03f482d6
    
  • Push the new history (only use --force if the below fails, and only after sanity checking with git log and/or git diff ).

    git push --force-with-lease
    

  • Github文档包含一个脚本,用于替换分支中所有提交的提交者信息。

    #!/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
    
    链接地址: http://www.djcxy.com/p/1414.html

    上一篇: 如何将私钥与钥匙串中的证书相关联

    下一篇: 在一个特定的提交中更改提交作者