Lorixen.
EN

Git commits under the wrong name or email? Fix it properly

Short answer: Git stamps every commit with the name and email from your configuration, and if none is set it guesses from your computer, which is how commits end up as sam@sams-laptop.local or under a personal address on a client project. Fix the configuration first, ideally with a separate work identity applied automatically to work folders. For commits you haven't pushed, amend or rebase them with the correct author. For history that's already shared, don't rewrite it; add a .mailmap file so logs and blame show the right name and email.

Why it happens

Each commit records an author and a committer, each with a name and email. Git takes them from user.name and user.email in your configuration. If they're missing, Git tries to build them from your system username and hostname. Common results:

  • a laptop hostname such as @MacBook-Pro.local in the email,
  • a personal address set globally years ago,
  • a previous employer's email still in the global config,
  • a mix of identities across team members' machines.

On hosting platforms, commits are usually linked to an account by email, so an unrecognised address can show as an unlinked author.

Step 1: stop Git guessing

Git's configuration docs describe user.useConfigOnly: when set to true, Git won't guess user.email and user.name, and will only use configured values. The docs give the example of people with several email addresses who want a different one per repository: with this option on in the global config, Git prompts you to set an email before you can commit in a repository that doesn't have one.

git config --global user.useConfigOnly true
git config --global user.name "Sam Patel"

Leaving the global email unset then forces a deliberate choice in each repository.

Step 2: apply work and personal identities automatically

Git can include extra configuration files based on conditions. The includeIf setting with a gitdir condition applies a file to repositories whose .git directory matches a path pattern. A typical arrangement:

[includeIf "gitdir:~/work/"]
    path = ~/.config/git/work.inc
[includeIf "gitdir:~/personal/"]
    path = ~/.config/git/personal.inc

Each included file sets its own user.email, and anything cloned under ~/work/ uses the work address. Freelancers often add a folder per client with that client's required address. Check the result with git config user.email inside a repository.

Step 3: fix commits you haven't pushed

For the latest commit, Git's documentation describes --reset-author: used with --amend, it declares that authorship of the resulting commit belongs to the committer, which is you with your now-correct config, and renews the author timestamp.

git commit --amend --reset-author --no-edit

For several unpushed commits, an interactive rebase can amend each one, or you can use a rebase that runs the same amend command after every commit. Either way you're rewriting commits that only exist on your machine, which is safe.

Step 4: shared history? Use .mailmap instead

Once commits are pushed and others have pulled them, rewriting them changes every commit ID after that point and forces everyone to recover their copies. For a wrong email, that's rarely worth it.

Git's answer is the .mailmap file. According to the documentation, if .mailmap exists at the top level of the repository, Git uses it to map author and committer names and email addresses to canonical ones. Commands that read it, such as git log with mailmap enabled, git shortlog and git blame, then show the correct identity without changing any commits.

The format supports several forms, including replacing just the email, or both the name and email, for commits matching an old address:

Sam Patel <sam@agency.example> <sam@MacBook-Pro.local>
Sam Patel <sam@agency.example> Sam P <sam.personal@example.com>

The docs note that names and emails are matched case-insensitively. Commit the .mailmap file so the whole team benefits.

When rewriting shared history is justified

There are occasional good reasons, such as a commit email that exposes a private address the person wants removed. In that case:

  • agree it with everyone who uses the repository,
  • use a dedicated history-rewriting tool and test on a fresh clone,
  • coordinate a time for everyone to re-clone or reset,
  • expect pull requests and links to old commit IDs to be affected.

Also consider whether your hosting provider offers a private "noreply" address for future commits, so the problem doesn't recur.

Check before you push

A quick look before pushing catches most mistakes. Run git log with a format that shows the author email for your unpushed commits, or check the output of git config user.email when you start work in a new clone. If something looks wrong, fix the configuration first, then amend, so the correction doesn't need repeating on the next commit.

Team-wide habits

  • Add identity setup to your onboarding notes, including the includeIf pattern.
  • Add a check in CI or a pre-receive rule that flags commit emails outside your organisation's domain, if that matters to you.
  • Keep .mailmap updated when people change names or addresses.

An illustrative example

A freelance developer in Bristol works for three agencies, each wanting commits under its own domain. Their commits had been going out under a personal address, and one agency's project showed a laptop hostname. They enable useConfigOnly, create a folder per agency with an includeIf file setting each address, amend two unpushed commits with --reset-author, and send each agency a .mailmap line so old commits display under the right identity.

FAQ

Does .mailmap change the commits?

No. It only changes how names and emails are displayed by commands that read it.

Why do my commits not link to my account on the hosting site?

The commit email probably isn't one registered on your account. Add it there or commit with an address that is.

Is the author the same as the committer?

Not always. The author wrote the change; the committer applied it, for example during a rebase or cherry-pick.

Will amending change the commit ID?

Yes. That's why it's safe only for commits you haven't shared.

If your team's commit history is a mix of identities and you'd like it tidied up safely, tell us how your repositories are set up.

Sources

WhatsAppBook a call