0

I used to have email A as my primary, but now have email B as my primary and do not use A at all. I changed my github primary email to B, but forgot to update my .gitconfig locally, so all the patch for all of my commits still say they were created by a user with email A.

Because of this, none of my commits are logged under my "Contributions" on github.

Official github response to this is to add email A to my github account, and my Contributions will automatically be rebuilt.

However, I would like to erase email A from every platform that I can - is there a way to instead change the "commiter" of every commit I have made to email B?

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40

1 Answers1

2

git filter-branch should be able to do this for you; essentially it rewrites the entire commit history of the branch with the same SHA1's.

There's an example script on Github that demonstrates how to do this; it's short, so I'm reproducing it here.

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-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

It will be necessary to force push the filtered branch, as you've changed history on it. If anyone is sharing your branch, that will make their history and yours mismatch -- you'll need to have all your collaborators re-pull the edited branch. If they've been working on the old branch and have made commits on it, they can cherry-pick those onto the new branch.

You may want to make this easier by creating a whole new branch and updating history there, then letting everyone pull it. At that point you can all agree that this is the new master branch and work on that basis, or simply have everyone delete their copy of the old master and rename the new branch to master.

Joe McMahon
  • 3,266
  • 21
  • 33
  • Thanks, perfect. It seems to have worked. Once I do this, do I have to `push`? Also, no one else has forked or cloned this thing, its a small personal repo for my own version control convenience. – pretzlstyle Nov 14 '17 at 20:51
  • 1
    @jphollowed - You'll need to use `git push --force`, since you are overwriting the history on your repo. – JDB Nov 14 '17 at 21:00
  • @JDB - I edited to incorporate your note, and clarified how anyone who'd been using the branch could catch up more easily. – Joe McMahon Nov 14 '17 at 23:30