Fix Wrong Email in Git Commits (Amend, Rebase, Force-Push)
To fix a commit made with the wrong email, set the right user.email, then run git commit --amend --reset-author --no-edit for the last commit, or git rebase <base> --exec "git commit --amend --reset-author --no-edit" for several. Changing the config alone never touches commits that already exist.
Short version: the email is baked into each commit. Fixing it means rewriting the commit, which gives it a new hash. That is harmless on a private branch and a problem on a shared one.
How do I check which email my commits used?
Look before you rewrite anything:
git log --format='%h %an <%ae> | committer: %ce' -10
%ae is the author email, %ce the committer email. GitHub attributes a commit by the author email, so that is the one you usually need to fix. If you only see the wrong address on some commits, note the oldest one: that is where your rewrite starts.
How do I fix the last commit?
Set the identity for this repo, then rebuild the commit with it:
git config user.email "you@work.example"
git config user.name "Your Name"
git commit --amend --reset-author --no-edit
--reset-author replaces the author with your current config and refreshes the author date. If you would rather not touch config, pass the identity explicitly:
git commit --amend --no-edit --author="Your Name <you@work.example>"
Note that --author changes only the author, not the committer.
How do I fix several commits at once?
Rebase onto the commit just before the first bad one, and amend each commit as it is replayed:
git rebase -r origin/main --exec "git commit --amend --reset-author --no-edit"
The --exec command runs after every commit is applied. -r keeps merge commits instead of flattening them. If the bad commits go back to the start of the repository, use git rebase -r --root --exec "...".
Check the result with the same git log command as above. Every line should show the right email.
What if the wrong email is all over the history?
For a whole repository, rewrite with git filter-repo and a mailmap file instead of a rebase. Create mailmap.txt:
Your Name <you@work.example> <you@personal.example>
Then, in a fresh clone (filter-repo refuses to run on a used one by default):
git filter-repo --mailmap mailmap.txt
Every commit from the old address now carries the new one. The older git filter-branch does the same job but is slower and its own documentation warns against it, so prefer filter-repo.
What if I already pushed?
Rewriting changes the hashes, so the remote will reject a normal push. Use the safer force:
git push --force-with-lease
--force-with-lease refuses to overwrite work you have not seen. Only do it on a branch that is yours. On a shared branch, agree on the rewrite with your team first, because everyone else must reset to the new history. On main, think twice: for one stray commit, the cost of the rewrite can exceed the cost of the leaked address.
If the problem is that a private address appeared in public history, rewriting your branch does not remove copies that others already fetched or that GitHub kept in old pull requests. Treat that address as exposed, and use your noreply address from now on.
How do I stop it happening again?
Fixing is the expensive path. The cheap one is making the right identity automatic per folder. Git can do this with includeIf, as covered in our guide, and you can add a safety net with a pre-commit hook that checks user.email. If your commits also show a gray badge afterwards, see how to fix unverified commits.
Hats does all of it from the menu bar: bind a folder to a profile and the commit email, SSH key, gh session and signing follow. If you forget anyway, its guard blocks the commit before the wrong email is written.
FAQ
Does changing user.email fix old commits?
No. user.email only applies to commits created from then on. Old commits keep their stored author until you rewrite them.
Will fixing the email change my commit hashes?
Yes. The author is part of what the hash is computed from, so every rewritten commit, and every commit after it, gets a new hash.
Why are my commits not linked to my GitHub profile?
GitHub matches the author email to an address on an account. If the email is not added to (and verified on) your account, the commit shows no avatar and does not count on your contribution graph. Add the address in your GitHub email settings, or rewrite the commits with an address that is.
Should I fix author or committer?
Author, in nearly every case. GitHub uses the author for attribution. Fix the committer only if you also want it consistent, for example by rebasing, which sets the committer to your current identity automatically.
Can I do this without force-pushing?
Only if the commits are not pushed yet. Once they are on the remote, correcting them means a force push. The alternative is leaving the history as it is and adding a .mailmap file so tools like git log and git shortlog display the right name.