Skip to content

Guide

Committed with the wrong email? How to fix it

A commit made with the wrong email is easy to fix while it's only on your Mac, and harder once it's pushed. Here's how to fix one commit or several, what --reset-author and --author each change, and how to make sure it doesn't happen again.

By the iKnowMyMac team · Updated · 7 min read

The short answer

Set the right email for the repo first, with git config user.email sam@acme.dev. Then fix the last commit with git commit --amend --no-edit --reset-author. For several commits, run git rebase -r <base> --exec 'git commit --amend --no-edit --reset-author', where <base> is the last commit that was right. Both rewrite history: fine before you push, but after a push you need git push --force-with-lease, and anyone who pulled the old commits has to reset to the new ones.

On this page

Check which email a commit used

Every commit records an author, who wrote it, and a committer, who last applied it. Usually they're the same. Show both for the last commit:

Author and committer of the last commit
git log -1 --format='author:    %an <%ae>%ncommitter: %cn <%ce>'

Then find where the wrong email came from, so the fix sticks:

Which file sets the email in this repo
git config --show-origin --get user.email

If it prints ~/.gitconfig, this repo has no email of its own and uses your global one. Set the right one for the repo with git config user.email sam@acme.dev, or for a whole folder with an includeIf rule (below).

Fix the last commit

Rewrite the last commit's author from the current config
git commit --amend --no-edit --reset-author

--reset-author takes the name and email from your config, so set them first. --no-edit keeps the message. If you'd rather name the author without changing config, use --author:

Set the author explicitly
git commit --amend --no-edit --author="Sam Lee <sam@acme.dev>"
  • Author name and email

    --reset-author
    From your current config
    --author="Name <email>"
    Exactly what you type
  • Author date

    --reset-author
    Reset to now
    --author="Name <email>"
    Kept
  • Committer

    --reset-author
    From your current config
    --author="Name <email>"
    Also from your current config, so it stays wrong until you fix the config
--reset-author compared with --author

Fix several commits

Rebase replays each commit after <base> and runs the amend after each one. Use the last good commit, or the branch you started from, as the base:

Every commit on this branch since origin/main
git rebase -r origin/main --exec 'git commit --amend --no-edit --reset-author'
Every commit since the root, for a repo that was never shared
git rebase -r --root --exec 'git commit --amend --no-edit --reset-author'

-r keeps merge commits as merges instead of flattening them. To change only the commits with the wrong email, list them first with git log --author=samdev@example.com --format='%h %ae %s'. To keep each commit's original date, use --author="Sam Lee <sam@acme.dev>" in the --exec command instead of --reset-author.

If you've already pushed

If the wrong email is yours, there's a fix that rewrites nothing: add the address to the right GitHub account under Settings → Emails and verify it. GitHub links commits to accounts by email, so the existing commits show up under that account.

If you do rewrite a pushed branch, collaborators update with git fetch and then git reset --hard origin/<branch>, which throws away any local commits they hadn't pushed, so they should rebase those onto the new branch first.

Stop it happening again

  • Set the email per folder with includeIf "gitdir:~/work/" in ~/.gitconfig, so new repos in that folder commit as work without anyone remembering.
  • Make Git refuse to guess. git config --global user.useConfigOnly true stops Git from inventing an email from your Mac's name. Combine it with no global email and Git refuses to commit until the repo or folder sets one.
  • Check before you push with a pre-push hook that compares each new commit's author with the repo's email.
.git/hooks/pre-push (make it executable with chmod +x)
#!/bin/sh
# Refuse to push commits whose author email differs from this repo's user.email.
expected=$(git config user.email)
zero=$(git hash-object --stdin </dev/null | tr '0-9a-f' '0')
while read -r local_ref local_sha remote_ref remote_sha; do
  [ "$local_sha" = "$zero" ] && continue
  if [ "$remote_sha" = "$zero" ]; then range="$local_sha --not --remotes"; else range="$remote_sha..$local_sha"; fi
  bad=$(git log --format=%ae $range | grep -v -x -F "$expected" | head -n 1)
  if [ -n "$bad" ]; then
    echo "pre-push: a commit is authored by $bad, but this repo uses $expected" >&2
    exit 1
  fi
done
exit 0

A repo has one pre-push hook. If you use husky, lefthook or the pre-commit framework, or git config core.hooksPath prints a folder, add the check to that tool's hook instead of replacing it.

The faster way: Wrong identity alerts in iKnowMyMac

iKnowMyMac checks each new commit's author against the profile for that repo, within seconds of the commit. If it's wrong, an alert names the repo and the email, and says whether the commit has been pushed.

FAQ

Questions

Something else? support@iknowmymac.com

Does --reset-author change the commit date?

Yes, it sets the author date to now. --author="Name <email>" keeps the original author date. Either way the committer date changes, because amending makes a new commit.

Will fixing the email change the commit hash?

Yes. The author is part of the commit, so every rewritten commit gets a new hash, and so does every commit after it on the branch. That's why a pushed branch needs git push --force-with-lease afterwards.

Can I fix the email on GitHub without rewriting history?

Only when the email is yours. Add and verify it in the right account's email settings, and GitHub links the existing commits to that account. The email stays in the commits themselves; only a rewrite changes that.

How do I find every commit made with the wrong email?

Run git log --all --author=samdev@example.com --format='%h %an <%ae> %s' with the wrong address. --author matches part of the name or email, so a domain such as @example.com works too.

Know why your Mac is slow.

macOS 15 Sequoia or later · Apple silicon. A 11.5 MB native app, notarized by Apple. No account, no analytics, no telemetry.

One-time purchase · every feature · 14-day refund