git - How to change the commit author for one specific commit?

ID : 281

viewed : 47

Tags : gitgit-commitgit

Top 5 Answer for git - How to change the commit author for one specific commit?

vote vote

92

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>" --no-edit 

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

  1. 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
  2. Change the lines for both C and D from pick to edit
  3. Exit the editor (for vim, this would be pressing Esc and then typing :wq).
  4. Once the rebase started, it would first pause at C
  5. You would git commit --amend --author="Author Name <email@address.com>"
  6. Then git rebase --continue
  7. It would pause again at D
  8. Then you would git commit --amend --author="Author Name <email@address.com>" again
  9. git rebase --continue
  10. The rebase would complete.
  11. Use git push -f to update your origin with the updated commits.
vote vote

88

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.

  1. Checkout the commit we are trying to modify.

     git checkout 03f482d6 
  2. Make the author change.

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

Now we have a new commit with hash assumed to be 42627abe.

  1. Checkout the original branch.

  2. Replace the old commit with the new one locally.

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

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

     git replace -d 03f482d6 
  5. 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 

Instead of 4-5 you can just rebase onto new commit:

git rebase -i 42627abe 
vote vote

70

Github documentation contains a script that replaces the committer info for all commits in a branch (now irretrievable, this is the last snapshot).

  • Run the following script from terminal after changing the variable values

    #!/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 
  • Push the corrected history to GitHub:

    git push --force --tags origin 'refs/heads/*' 

OR if you like to push selected references of the branches then use

``` git push --force --tags origin 'refs/heads/develop' ``` 
vote vote

63

  • 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

vote vote

59

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.

Top 3 video Explaining git - How to change the commit author for one specific commit?

Related QUESTION?