How to change the commit author for one specific commit?

后端 未结 19 1855
没有蜡笔的小新
没有蜡笔的小新 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:42

    OPTIONAL: Make sure to stash your local changes if you don't want to send them to remote.

    $ git status
    $ git stash
    

    Update the author for the last commit.

    $ git log   // Old author in local and remote
    $ git commit --amend --author="Author Name <email@address.com>"
    $ git log   // New Author in local
    $ git push origin <branch> --force-with-lease 
    $ git log   // New Author in remote
    

    Then, if you used git stash then recovers your staged changes

    $ git stash pop
    $ git status
    

    Then, you should to update the configuration for the next commits of the current project.

    $ git config user.name "Author Name"
    $ git config user.email "<email@address.com>"
    

    And check or also edit this with git config --edit


    Clarification: In the rare case that you lose commits using $ ggpush -f you can recover them with reflog. Anyway using --force-with-lease you are protected even more than if you use only -f

    GL

    • Source
    • ZSH
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 05:44
    • Reset your email to the config globally:

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

    • Now reset the author of your commit without edit required:

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

    0 讨论(0)
  • 2020-11-22 05:44

    For the merge commit message, I found that I cannot amend it by using rebase, at least on gitlab. It shows the merge as a commit but I cannot rebase onto that #sha. I found this post is helpful.

    git checkout <sha of merge>
    git commit --amend # edit message
    git rebase HEAD previous_branch
    

    This three lines of code did the job for changing the merge commit message (like author).

    0 讨论(0)
  • 2020-11-22 05:45

    You can change author of last commit using the command below.

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

    However, if you want to change more than one commits author name, it's a bit tricky. You need to start an interactive rebase then mark commits as edit then amend them one by one and finish.

    Start rebasing with git rebase -i. It will show you something like this.

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

    Change the pick keyword to edit for the commits you want to change the author name.

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

    Then close the editor. For the beginners, hit Escape then type :wq and hit Enter.

    Then you will see your terminal like nothing happened. Actually you are in the middle of an interactive rebase. Now it's time to amend your commit's author name using the command above. It will open the editor again. Quit and continue rebase with git rebase --continue. Repeat the same for the commit count you want to edit. You can make sure that interactive rebase finished when you get the No rebase in progress? message.

    0 讨论(0)
  • 2020-11-22 05:46

    Find a way that can change user quickly and has no side effect to others commits.

    Simple and clear way:

    git config user.name "New User"
    git config user.email "newuser@gmail.com"
    
    git log
    git rebase -i 1f1357
    # change the word 'pick' to 'edit', save and exit
    
    git commit --amend --reset-author --no-edit
    git rebase --continue
    
    git push --force-with-lease
    

    detailed operations

    • show commit logs and find out the commit id that ahead of your commit which you want to change:
    git log
    
    • git rebase start from the chosed commit id to the recent reversely:
    git config user.name "New User"
    git config user.email "newuser@gmail.com"
    git rebase -i 1f1357
    
    # change word pick to edit, save and exit
    edit 809b8f7 change code order 
    pick 9baaae5 add prometheus monitor kubernetes
    edit 5d726c3 fix liquid escape issue   
    edit 3a5f98f update tags
    pick 816e21c add prometheus monitor kubernetes
    
    • rebase will Stopped at next commit id, output:
    Stopped at 809b8f7...  change code order 
    You can amend the commit now, with
      git commit --amend 
    
    Once you are satisfied with your changes, run
    
      git rebase --continue
    
    • comfirm and continue your rebase untill it successfully to refs/heads/master.
    # each continue will show you an amend message
    # use git commit --amend --reset-author --no-edit to comfirm
    # use git rebase --skip to skip
    git commit --amend --reset-author --no-edit
    git rebase --continue
    git commit --amend --reset-author --no-edit
    ...
    git rebase --continue
    Successfully rebased and updated refs/heads/master.
    
    • git push to update
    git push --force-with-lease
    
    0 讨论(0)
提交回复
热议问题