How to change the commit author for one specific commit?

后端 未结 19 2156
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:15

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

19条回答
  •  野性不改
    2020-11-22 05:43

    When doing git rebase -i there is this interesting bit in the doc:

    If you want to fold two or more commits into one, replace the command "pick" for the second and subsequent commits with "squash" or "fixup". If the commits had different authors, the folded commit will be attributed to the author of the first commit. The suggested commit message for the folded commit is the concatenation of the commit messages of the first commit and of those with the "squash" command, but omits the commit messages of commits with the "fixup" command.

    • If you have an history of A-B-C-D-E-F,
    • and you want to change commits B and D (= 2 commits),

    then you can do:

    • git config user.name "Correct new name"
    • git config user.email "correct@new.email"
    • create empty commits (one for each commit):
      • you need a message for rebase purpose
      • git commit --allow-empty -m "empty"
    • start the rebase operation
      • git rebase -i B^
      • B^ selects the parent of B.
    • you will want to put one empty commit before each commit to modify
    • you will want to change pick to squash for those.

    Example of what git rebase -i B^ will give you:

    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 that to:

    # 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
    

    It will prompt you to edit the messages:

    # 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...
    

    and you can just remove the first few lines.

提交回复
热议问题