2

I've changed my Git author author name from "First Last <myemail@email.com>" to "First Last <otheremail@email.com>"

The two email addresses are associated with different Github accounts and I'm migrating all my personal projects to the second one.

My problem is that all my past work on some private repositories (I'm the only contributor) was done using the first account. The migrated code appears to have been committed by some other user. How can I force change all the commits to use my new Git author name??

If I can do that, I can force push the change to Github and the work will appear to have all been done by the user First Last , which is what I want.

Thanks!

dgh
  • 8,969
  • 9
  • 38
  • 49

2 Answers2

3

The easiest way is to:

I would recommend this solution, which doesn't blindly change the commit (in case you incorporated patches from other developers)

#!/bin/bash

git filter-branch --env-filter '
if [ "$GIT_AUTHOR_NAME" = "<old author>" ];
then
    GIT_AUTHOR_NAME="<new author>";
    GIT_AUTHOR_EMAIL="<youmail@somehost.ext>";
fi
if [ "$GIT_COMMITTER_NAME" = "<old committer>" ];
then
    GIT_COMMITTER_NAME="<new commiter>";
    GIT_COMMITTER_EMAIL="<youmail@somehost.ext>";
fi
' -- --all
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You can use git filter-branch for your purposes; specifically, its env-filter.

--env-filter <command>
  This filter may be used if you only need to modify the environment in which
  the commit will be performed. Specifically, you might want to rewrite
  the author/committer name/email/time environment variables (see
  git-commit-tree(1) for details). Do not forget to re-export the variables.

Something like:

git filter-branch \
  --env-filter 'export GIT_AUTHOR_NAME="First Last";
                export GIT_AUTHOR_EMAIL="otheremail@email.com";
                export GIT_COMMITTER_NAME="${GIT_AUTHOR_NAME}";
                export GIT_COMMITTER_EMAIL="${GIT_AUTHOR_EMAIL}";' \
  -- --all

If you don't need to change your author name, just drop the GIT_AUTHOR_NAME and GIT_COMMITTER_NAME assigments.

Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48