How to Rewrite Git Commit History to Fix Incorrect Author/Committer Emails


2 views

Many developers encounter this issue when they first start using Git - their early commits show up with local machine credentials (like user@localhost) instead of their proper Git identity. This happens because:

# Typical output showing the problem
git log --pretty=format:"%h - %an, %ae, %ad"

# Example bad output:
# a1b2c3d - cowens, cowens@localmachine, Fri Jan 5 14:30:22 2023

First, configure your correct identity globally or per-repository:

# Global configuration
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Or for a specific repository only:
git config user.name "Your Name"
git config user.email "your.email@example.com"

To fix historical commits, we'll use git filter-branch or git filter-repo (recommended for newer Git versions). Here's how:

# Using filter-branch (works on all Git versions)
git filter-branch --env-filter '
OLD_EMAIL="cowens@localmachine"
CORRECT_NAME="Your Name"
CORRECT_EMAIL="your.email@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 -- --all

For larger repositories, git-filter-repo is faster and safer:

# First install git-filter-repo
pip install git-filter-repo

# Then run the email replacement
git filter-repo --mailmap <<EOF
cowens@localmachine <your.email@example.com> Your Name
EOF

If you've already pushed to remote repositories, you'll need to force push after rewriting history:

git push --force --tags origin 'refs/heads/*'

Warning: Force pushing rewrites history for all collaborators. Coordinate with your team before doing this.

After rewriting, verify the changes took effect:

git log --pretty=format:"%h - %an, %ae, %ad"

For projects where you can't rewrite history, create a .mailmap file:

Your Name <your.email@example.com> cowens <cowens@localmachine>

This provides a non-destructive way to normalize author information.


When you first initialize a Git repository with git init, Git uses your system's default username and email configuration. Many developers (myself included) have found themselves with commits showing undesired identifiers like cowens@localmachine instead of their proper professional email.

First, let's ensure future commits have the correct information:

git config --global user.name "Your Correct Name"
git config --global user.email "your.email@example.com"

This sets your global Git configuration. For repository-specific settings, omit the --global flag.

To modify existing commits, we'll use Git's powerful filter-branch command. This creates new commit objects with corrected metadata while maintaining the actual code changes.

Create this shell script (let's call it correct-emails.sh):

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="cowens@localmachine"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your.email@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 -- --all

Make it executable and run it:

chmod +x correct-emails.sh
./correct-emails.sh

After running the script, verify the changes with:

git log --pretty=format:"%h %an <%ae>"

This will show the commit hash, author name, and author email for each commit.

If you've already pushed these commits to a remote repository, you'll need to force push:

git push --force --tags origin 'refs/heads/*'

Warning: This rewrites history on the remote, which affects all collaborators. Coordinate with your team before doing this.

For smaller repositories or targeted changes, interactive rebase might be preferable:

git rebase -i HEAD~10 --exec 'git commit --amend --reset-author --no-edit'

This will rewrite the last 10 commits with your current author information.

1. Rewriting history changes commit SHAs, potentially breaking references to old commits
2. Signed commits will become invalid as their content changes
3. Any work based on the old commits will need to be rebased