Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
@gchicareli

This comment has been minimized.

Show comment
Hide comment
@gchicareli

gchicareli Sep 21, 2014

git filter-branch --env-filter '

OLD_EMAIL="chicareligerson@icloud.com"
CORRECT_NAME="Gerson Chicareli"
CORRECT_EMAIL="chicareligerson@icloud.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

git filter-branch --env-filter '

OLD_EMAIL="chicareligerson@icloud.com"
CORRECT_NAME="Gerson Chicareli"
CORRECT_EMAIL="chicareligerson@icloud.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

@Zemke

This comment has been minimized.

Show comment
Hide comment
@finnoleary

This comment has been minimized.

Show comment
Hide comment
@finnoleary

finnoleary Apr 25, 2015

Line three
git filter-branch --env-filter '
should be changed to
git filter-branch --env-filter $'
to escape ' it in the case that CORRECT_NAME should contain an apostrophe.

Line three
git filter-branch --env-filter '
should be changed to
git filter-branch --env-filter $'
to escape ' it in the case that CORRECT_NAME should contain an apostrophe.

@shivapoudel

This comment has been minimized.

Show comment
Hide comment
@shivapoudel

shivapoudel Sep 1, 2015

What if we have multiple old emails? This though might be useful:

#!/bin/sh
# see https://help.github.com/articles/changing-author-info/

git filter-branch --env-filter '

OLD_EMAIL=(
    "your-old-email@example.com"
    "your-git-email@users.noreply.github.com"
)
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

for NEW_EMAIL in ${OLD_EMAIL[@]}; do
    if [ "$GIT_COMMITTER_EMAIL" == "$NEW_EMAIL" ]; then
        export GIT_COMMITTER_NAME="$CORRECT_NAME"
        export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" == "$NEW_EMAIL" ]; then
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
done
' --tag-name-filter cat -- --branches --tags

What if we have multiple old emails? This though might be useful:

#!/bin/sh
# see https://help.github.com/articles/changing-author-info/

git filter-branch --env-filter '

OLD_EMAIL=(
    "your-old-email@example.com"
    "your-git-email@users.noreply.github.com"
)
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

for NEW_EMAIL in ${OLD_EMAIL[@]}; do
    if [ "$GIT_COMMITTER_EMAIL" == "$NEW_EMAIL" ]; then
        export GIT_COMMITTER_NAME="$CORRECT_NAME"
        export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" == "$NEW_EMAIL" ]; then
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
done
' --tag-name-filter cat -- --branches --tags
@drAlberT

This comment has been minimized.

Show comment
Hide comment
@drAlberT

drAlberT Sep 10, 2015

@shivapoudel good enhancement 👍

It could be done a case insensitive OLD_EMAIL search too (see my fork)

@shivapoudel good enhancement 👍

It could be done a case insensitive OLD_EMAIL search too (see my fork)

@oshingc

This comment has been minimized.

Show comment
Hide comment
@oshingc

oshingc Mar 23, 2016

@gchicareli Don't work for me I have this
ssh

oshingc commented Mar 23, 2016

@gchicareli Don't work for me I have this
ssh

@oshingc

This comment has been minimized.

Show comment
Hide comment
@oshingc

oshingc Mar 23, 2016

My console prints this, can someone help me please?
errorrr

oshingc commented Mar 23, 2016

My console prints this, can someone help me please?
errorrr

@caipivara

This comment has been minimized.

Show comment
Hide comment
@caipivara

caipivara Apr 20, 2016

It worked as it is for me but I had to checkout every branch manually and run it.

It worked as it is for me but I had to checkout every branch manually and run it.

@tardypad

This comment has been minimized.

Show comment
Hide comment
@tardypad

tardypad Aug 3, 2016

I've moved one of my personal script related to this and made a command for it in https://github.com/tj/git-extras/
See https://github.com/tj/git-extras/blob/master/man/git-reauthor.md

tardypad commented Aug 3, 2016

I've moved one of my personal script related to this and made a command for it in https://github.com/tj/git-extras/
See https://github.com/tj/git-extras/blob/master/man/git-reauthor.md

@frz-dev

This comment has been minimized.

Show comment
Hide comment
@frz-dev

frz-dev May 3, 2017

In case you also want to change commits with a specific author name:

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
OLD_NAME="You Old Name"
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_NAME" = "$OLD_NAME" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_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

My fork: https://gist.github.com/frz-dev/adf8c2c7275da1369e0cc340feda0ba0

frz-dev commented May 3, 2017

In case you also want to change commits with a specific author name:

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
OLD_NAME="You Old Name"
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_NAME" = "$OLD_NAME" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_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

My fork: https://gist.github.com/frz-dev/adf8c2c7275da1369e0cc340feda0ba0

@pigflymoon

This comment has been minimized.

Show comment
Hide comment
@pigflymoon

pigflymoon May 23, 2017

@frz-dev Awesome! Thanks

@frz-dev Awesome! Thanks

@VaibhavMisra

This comment has been minimized.

Show comment
Hide comment
@VaibhavMisra

VaibhavMisra Aug 4, 2017

On mac when I added quotes around my OLD_EMAIL, etc. in quotes some extra characters got added in my name (escape chars?). Then I had to make my OLD_EMAIL as my CORRECT_EMAIL with quotes and add CORRECT_NAME and CORRECT_EMAIL without quotes. This resolved everything and linked my commits properly.

On mac when I added quotes around my OLD_EMAIL, etc. in quotes some extra characters got added in my name (escape chars?). Then I had to make my OLD_EMAIL as my CORRECT_EMAIL with quotes and add CORRECT_NAME and CORRECT_EMAIL without quotes. This resolved everything and linked my commits properly.

@Avinash-Bhat

This comment has been minimized.

Show comment
Hide comment
@Avinash-Bhat

Avinash-Bhat Aug 17, 2017

This is my stab at making this script a bit more usable:

TL;DR?

the following changes makes it possible to use the script like this:

> git rewrite-author your-old-email@example.com your-name your-new-email@example.com

changes to ~/.gitconfig

[alias]
    ...
    rewrite-author = !/<path-to-script>/git-rewrite-author.sh

Note: Change the <path-to-script> with the actual path of the script

changed script (git-rewrite-author.sh)

#!/bin/sh

OLD_EMAIL=$1
CORRECT_NAME=$2
CORRECT_EMAIL=$3
shift 3
if [ -z "$OLD_EMAIL" ]; then
	echo "old email is missing"
	exit 1
fi
if [ -z "$CORRECT_NAME" ]; then
	echo "correct name is missing"
	exit 2
fi
if [ -z "$CORRECT_EMAIL" ]; then
	echo "correct email is missing"
	exit 3
fi
echo "re-writing history of '${OLD_EMAIL}' to '${CORRECT_NAME}'(${CORRECT_EMAIL})"
git filter-branch --env-filter "
	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

PS: if you need to force the change (because git usually stores the original refs under refs/original/), use -f to force rewrite.

Avinash-Bhat commented Aug 17, 2017

This is my stab at making this script a bit more usable:

TL;DR?

the following changes makes it possible to use the script like this:

> git rewrite-author your-old-email@example.com your-name your-new-email@example.com

changes to ~/.gitconfig

[alias]
    ...
    rewrite-author = !/<path-to-script>/git-rewrite-author.sh

Note: Change the <path-to-script> with the actual path of the script

changed script (git-rewrite-author.sh)

#!/bin/sh

OLD_EMAIL=$1
CORRECT_NAME=$2
CORRECT_EMAIL=$3
shift 3
if [ -z "$OLD_EMAIL" ]; then
	echo "old email is missing"
	exit 1
fi
if [ -z "$CORRECT_NAME" ]; then
	echo "correct name is missing"
	exit 2
fi
if [ -z "$CORRECT_EMAIL" ]; then
	echo "correct email is missing"
	exit 3
fi
echo "re-writing history of '${OLD_EMAIL}' to '${CORRECT_NAME}'(${CORRECT_EMAIL})"
git filter-branch --env-filter "
	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

PS: if you need to force the change (because git usually stores the original refs under refs/original/), use -f to force rewrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment