How to amend several commits in Git to change author

前端 未结 6 1002
逝去的感伤
逝去的感伤 2020-11-28 17:14

I have made a series of commits in Git and I realise now that I forgot to set my user name and user email properties correctly (new machine). I have not yet pushed these co

6条回答
  •  [愿得一人]
    2020-11-28 18:04

    The interactive rebase approach is pretty nice when used in conjunction with exec. You can run any shell command against a specific commit or all commits in the rebase.

    First set your git author settings

    git config --global user.name "John Doe"
    git config --global user.email johndoe@example.com
    

    Then to reset the author for all commits after the given SHA

    git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"
    

    This will pop up your editor to confirm the changes. All you need to do here is save and quit and it will go through each commit and run the command specified in the -x flag.

    Per @Dave's comment below, you can also change the author while maintaining the original timestamps with:

    git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name ' -CHEAD"
    

提交回复
热议问题