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

git

1 September, 2021



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, which doesn't require you to re-type your author details, but apparently it won't keep the commit timestamps:

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



0 comments

Leave a comment