DEV Community

Cover image for How I Cleaned Up My Git History Like a Boss (a.k.a. Fixing Wrong Author Emails)
Emrah G.
Emrah G.

Posted on

How I Cleaned Up My Git History Like a Boss (a.k.a. Fixing Wrong Author Emails)

Sometimes you commit code like a beast...

But sometimes you commit with the wrong identity 😅

That happened to me.

I accidentally pushed a bunch of commits to several of my GitHub repos using a secondary email identity instead of my personal one.

The result? My beautiful Git history was sprinkled with something like:

Author: John Doe <old.email@company.com>
Enter fullscreen mode Exit fullscreen mode

Nope. That’s not what I want to be remembered for.

So here’s how I fixed my Git history like a time-traveling pro — and how you can do it too.


🛠️ Step 1: The Magic Script – git filter-branch

This little script saved my soul:

git filter-branch --env-filter '
OLD_EMAIL="old.email@company.com"
CORRECT_NAME="Correct Name"
CORRECT_EMAIL="new.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 -- --branches --tags
Enter fullscreen mode Exit fullscreen mode

This rewrites all commits in your repo that match the old email and replaces them with the correct name/email combo.


⚠️ Step 2: Push With Caution

Since we’re rewriting history, we need to force push the updated commits:

git push --force --tags origin 'refs/heads/*'
Enter fullscreen mode Exit fullscreen mode

Yes, it’s dangerous.

Yes, it breaks things if others are working on the same repo.

But in my case, it was a solo project. I made sure I wasn’t nuking anyone else's work.


👀 Before & After

Before:

commit 1234567
Author: John Doe <old.email@company.com>
Enter fullscreen mode Exit fullscreen mode

After:

commit 1234567
Author: Correct Name <new.email@example.com>
Enter fullscreen mode Exit fullscreen mode

Peace restored ✌️


🧠 Lessons Learned

  • Always check your Git config with git config user.email before committing.
  • Consider setting repo-specific Git configs if you use multiple identities.
  • If you mess up, don’t panic. Git is powerful enough to undo your mistakes — if you know how to wield it.

📚 Bonus: My Related Post

If you're into Git magic, you might also enjoy:

👉 How to Squash Like a Grand Master


Happy hacking, and may your Git logs forever reflect your true self!

Top comments (2)

Collapse
 
jnareb profile image
Jakub Narębski

First, you can correct the visible e-mail with .mailmap file, without the need to rewrite history, by adding the following line to it

Correct Name <new.email@example.com> John Doe <old.email@company.com>
Enter fullscreen mode Exit fullscreen mode

Second, git filter-branch is deprecated in favor of git filter-repo.

Collapse
 
emrahg profile image
Emrah G.

Thank you Jakub. Yes you are right in both items.

For anyone who visits this comment please be aware mailmap does not change the real contributor in Github. It just changes the visible e-mail. You need to use the approach mentioned in the post to change the contributor (commit owner) in a repo (you can also use git filter-repo as Jakub mentioned).