How to change Author of a previous Git Commit.

How to change Author of a previous Git Commit.

PURPOSE

To change the Author of a previous commit in a Repository.

Many times you end up commiting and pushing changes to a repository from a different account than you originally intended to do. You can ignore that commit, and move on in life Or You can follow these steps below:

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. Open Terminal.

  2. Change the current working directory to your local project.

  3. Checkout the commit we are trying to modify.

git checkout 03f482d6
  1. Make the author change.
git commit --amend --author "New Author Name <New Author Email>"
  1. Now we have a new commit with hash assumed to be 42627abe.

  2. Checkout the original branch. For example if the original branch was master.

git checkout master
  1. Replace the old commit with the new one locally.
git replace 03f482d6 42627abe
  1. Rewrite all future commits based on the replacement.
git filter-branch -- --all
  1. Remove the replacement for cleanliness.
git replace -d 03f482d6
  1. 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

Note: Instead of 7-9 you can just rebase onto new commit:

git rebase -i 42627abe

CREDITS

This article is from a StackOverflow Answer here I am adding it here to quickly reference it when I inevitably forget how to do it.


Tagged in git