Post

Git: Replace a Repo's History with One Commit Under a New Identity

Git: Replace a Repo's History with One Commit Under a New Identity

Sometimes a repo’s history carries a name or email you don’t want published: a work address in a personal project, or your real name in a repo you’d rather keep under a handle. If you don’t need the old history, the quickest fix is to replace it with a single fresh commit made under the right identity.

Quick tip: If you absolutely need to preserve the history, install the git-filter-repo tool and run git filter-repo --mailmap: it rewrites the author and committer on every commit instead of collapsing them into one.

In the examples below, the old identity is Jane Doe <jane.doe@work.example> and the new one is janed <janed@example.org>.

1. Set the new identity for this repo only

Navigate to the repo and execute the following:

1
2
$ git config user.name "janed"
$ git config user.email "janed@example.org"

Leave out --global, so your other repos keep their own identity.

2. Make a one-commit branch

git checkout --orphan creates a branch with no history but keeps all your files staged, so a single commit captures the current state of the project:

1
2
$ git checkout --orphan clean
$ git commit -m "Version 1.0.0."

3. Check both the author and the committer

Every commit records two identities: the author and the committer. Changing only one (for example, with git commit --amend --author=...) leaves the old name in the other, so check both:

1
2
3
4
$ git log --all --format="%h %d %an <%ae> | %cn <%ce> %s"
921459c  (HEAD -> clean) janed <janed@example.org> | janed <janed@example.org> Version 1.0.0.
d110bcf  (master) Jane Doe <jane.doe@work.example> | Jane Doe <jane.doe@work.example> Fixed choppy scrolling.
...

Both halves of the new commit should show the new identity.

4. Replace master

1
2
3
$ git branch -D master            # delete the old history
$ git branch -m clean master      # rename the clean branch
$ git push --force -u origin master

Delete any other branches and tags that point at old commits as well:

1
2
3
4
$ git branch -D <branch>
$ git push origin --delete <branch>
$ git tag -d <tag>
$ git push origin --delete <tag>

Afterwards, git log --all should show only the new commit.

5. Purge the old commits locally

The old commits stay on disk, reachable through the reflog, until git cleans them up. They won’t be pushed, since no branch points at them anymore; however, it is prudent to remove them:

1
$ git reflog expire --expire=now --all && git gc --prune=now

Things to know about GitHub

  • GitHub links commits to accounts by email.
    A commit shows up under whichever account has its email address verified. Make sure the new email isn’t verified on an account under your old name, or use the new account’s @users.noreply.github.com address.
  • A force push doesn’t erase what was already published.
    Old commits can stay reachable by their hash for a while, and forks and pull requests keep their own copies. If the old history was ever public, the cleanest option is to delete the repository on GitHub and push the new history to a fresh one. Otherwise, see GitHub’s guide on removing sensitive data.

Not committing as the wrong person in the first place

Wrong identities end up in history because git quietly uses whatever identity it finds: usually the global one, which is often the wrong one. This happens because git quietly uses whatever identity it finds: usually the global one, which is often the wrong one.
For that specific purpose, I created git-identity, a small tool for Windows (PowerShell 7 and Git for Windows), to make switching identities per repo a one-liner.

Here’s how it works.

  1. You define your identities once in ~/.git-identities.json:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
     {
       "profiles": {
         "work": { 
           "name": "Jane Doe",   
           "email": "jane.doe@work.example",  
           "sshKey": "~/.ssh/id_ed25519" 
         },
         "personal": { 
           "name": "janed",    
           "email": "janed@example.org",     
           "sshKey": "~/.ssh/id_ed25519_personal" 
         }
       }
     }
    
  2. Then you apply one inside any repo:

    1
    2
    
     $ git identity personal
     Switched local identity to personal (janed <janed@example.org>)
    

A profile sets user.name and user.email and, optionally, an SSH key for pushing and pulling and a key for signing commits. Running git identity on its own shows which identity the repo is using and whether it’s the repo’s own setting or inherited from your global config. It only writes ordinary git config values, so everything it does can be inspected or undone with git config.

The most useful part works even without the tool: tell git to refuse commits until a repo has an identity of its own.

1
2
3
$ git config --global --unset user.name
$ git config --global --unset user.email
$ git config --global user.useConfigOnly true

From then on, any repo without its own identity, including existing ones, refuses to commit until you set one, either with git identity <profile> or with plain git config user.name and git config user.email.

This post is licensed under CC BY 4.0 by the author.