dgh
dgh

Reputation: 9137

Override All Git Authors After Changing Accounts

I've changed my Git author author name from "First Last <[email protected]>" to "First Last <[email protected]>"

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!

Upvotes: 2

Views: 428

Answers (2)

VonC
VonC

Reputation: 1324987

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="<[email protected]>";
fi
if [ "$GIT_COMMITTER_NAME" = "<old committer>" ];
then
    GIT_COMMITTER_NAME="<new commiter>";
    GIT_COMMITTER_EMAIL="<[email protected]>";
fi
' -- --all

Upvotes: 3

Marco Leogrande
Marco Leogrande

Reputation: 8468

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="[email protected]";
                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.

Upvotes: 1

Related Questions