How to change the author details for historical commits in a git repository

1 September, 2021
git

tl;dr git rebase -i --root -x "git commit --amend --author 'New Name <new-email@example.com>' -CHEAD"

Update git config

git config --global user.name "New Name"
git config --global user.email new-email@example.com

Amend commit history

git rebase -i --root -x "git commit --amend --author 'New Name <new-email@example.com>' -CHEAD"

Okay, we maybe didn't need to update the git config, but it's probably a good idea for the future anyway. We could also do this version with --reset-author instead of --author <author details>, but be aware that this will overwrite the commit timestamps to the current time:

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

The --root flag targets all commits in the repository's history. You can target specific commits with SHA keys, or replace it with HEAD~N where N is an integer describing the number of commits to go back to.

Push

git push --force

Or if you're just pushing to a new repo, you won't need to force, and can just, e.g., git push -u origin main.

Remote

Maybe you need to do this because you're changing your remote and want to change your email at the same time. You could keep your old remote and just rename it, but I prefer to just remove it entirely, e.g.,

git remote set-url origin path/to/git/repo.git

There's a lot more detail in the Stack Overflow posts in the references.

References