The Time Traveler’s Guide: How to Change Git Commit Authors
Rewriting history in Git is a bit like time travel: it's powerful, but if you're not careful, you can create a messy alternate timeline. Since Git commits are cryptographically linked to their parent, changing an author changes the commit hash, which means you are technically replacing old commits with brand-new ones.
🛠️ 1. The "Quick Fix" (Most Recent Commit)
If you just realized your last commit has the wrong email, you don't need fancy tools. You can simply "amend" the last commit.
-
Manual Override:
git commit --amend --author="New Name <newemail@example.com>" --no-edit -
The "Reset to My Config" Shortcut: If you've already fixed your
git configand just want the last commit to match it:git commit --amend --reset-author --no-edit
🕵️ 2. The "Surgeon’s Choice" (Specific Past Commits)
If the mistake is 3 or 4 commits back, use Interactive Rebase. This allows you to pause time at a specific point in the past, change the author, and then resume.
-
Find how many commits back the mistake is (e.g., 5 commits):
git rebase -i HEAD~5 -
In the text editor that opens, change the word
picktoedit(or juste) next to the bad commit. Save and exit. -
Git will stop at that commit. Now run:
git commit --amend --author="Correct Name <email@example.com>" --no-edit -
Tell Git to finish the job:
git rebase --continue
🚀 3. The "Mass Update" (Entire History)
If you’ve been using the wrong email for months and want to fix every single commit in the repository, you should use git-filter-repo.
Note: This is the modern replacement for the old
git filter-branch.
Step 1: Create a "Mailmap" file
Create a file named my-mailmap with this format:
Correct Name <correct@email.com> <wrong@email.com>
Step 2: Run the filter git filter-repo --mailmap my-mailmap
🛡️ 4. The "Legacy Script" (No Installation Needed)
If you can't install new tools and need a "vanilla" Git solution for bulk changes, you can use the classic (though slower) filter-branch script:
git filter-branch --env-filter '
OLD_EMAIL="<wrong@example.com>"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="<correct@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
⚠️ The Golden Rule: The Force Push
Because these commands rewrite history (change hashes), the remote server (GitHub/GitLab) will reject a normal git push. You will have to Force Push:
git push --force
Danger Zone: Never do this on a branch that other people are actively working on without telling them first. Their local history will no longer match the server, and they will get errors!
📚 Comparison of Methods
| Method | Scope | Safety | Best For... |
|---|---|---|---|
| --amend | Last commit only | High | Typos made 1 minute ago. |
| rebase -i | A few specific commits | Medium | Fixing a specific session's mistakes. |
| filter-repo | Entire Repository | Low | Migrating from a personal to work email. |
🛡️ Preventing Future Mistakes: Conditional Includes
The best way to "change" an author is to never get it wrong in the first place. You can set up your global .gitconfig to automatically switch your email based on which folder you are working in (e.g., ~/work/ vs ~/personal/).
