How to Fix Wrong Git Commit Author Email Without Re‑committing
This guide explains why commits may contain an incorrect corporate email, shows how to amend the most recent commit or batch‑rewrite the entire history using git commands and a filter‑branch script, and addresses a common backup error during the rewrite process.
Problem Overview
When a repository requires a corporate email address, an incorrect user.email setting can cause commits to have the wrong author email. Instead of re‑committing, the author information can be rewritten in existing history.
Amending the Most Recent Commit
Use git commit --amend with the --author option to replace the author name and email of the tip commit.
git commit --amend --author="NewAuthor <[email protected]>"After amending, run git push --force if the commit has already been pushed to a remote.
Rewriting Email Across the Entire History
For many commits, git filter-branch (or the newer git filter-repo) can rewrite author and committer metadata. The following shell script demonstrates the --env-filter approach. Replace the placeholders with the actual old and new values, then execute the script from the repository root.
#!/bin/sh
git filter-branch --env-filter '
old_email="[Your Old Email]"
new_name="[Your New Author Name]"
new_email="[Your New Email]"
if [ "$GIT_AUTHOR_EMAIL" = "$old_email" ]; then
export GIT_AUTHOR_NAME="$new_name"
export GIT_AUTHOR_EMAIL="$new_email"
fi
if [ "$GIT_COMMITTER_EMAIL" = "$old_email" ]; then
export GIT_COMMITTER_NAME="$new_name"
export GIT_COMMITTER_EMAIL="$new_email"
fi
' -- --allAfter the rewrite, verify the changes with git log --pretty=format:"%h %an %ae". Force‑push the rewritten history to the remote (e.g., git push --force --tags origin master). Be aware that rewriting history changes commit hashes, which can disrupt collaborators.
Handling Existing Backup References
git filter-branchcreates a backup under refs/original/. If a previous run left a backup, the command will abort with “A previous backup already exists in refs/original/”. Remove the stale backup before re‑running:
# Delete the specific backup reference
git update-ref -d refs/original/refs/heads/master
# Or delete the whole original namespace
git update-ref -d refs/originalAlternatively, you can force the filter‑branch operation with the -f flag.
Alternative Tool: git filter-repo
For large repositories, git filter-repo is faster and safer. The equivalent operation is: git filter-repo --mailmap my-mailmap.txt where my-mailmap.txt contains lines of the form:
New Name <[email protected]> <[email protected]>References
Git documentation for git commit --amend and git filter-branch GitHub guide: “Changing author info in existing commits”
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
