Skip to content

Instantly share code, notes, and snippets.

@octocat
Last active September 4, 2024 20:14
Show Gist options
  • Save octocat/0831f3fbd83ac4d46451 to your computer and use it in GitHub Desktop.
Save octocat/0831f3fbd83ac4d46451 to your computer and use it in GitHub Desktop.
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
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
@oshingc
Copy link

oshingc commented Mar 23, 2016

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

@danielgomezrico
Copy link

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

@tardypad
Copy link

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

@fed-franz
Copy link

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

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
OLD_NAME="You Old Name"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
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
Copy link

@frz-dev Awesome! Thanks

@VaibhavMisra
Copy link

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
Copy link

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 [email protected] your-name [email protected]

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.

@fed-franz
Copy link

I also wrote a convenient script to easily rewrite author/committer name and/or email.
You can find it here:
https://github.com/frz-dev/utilities/blob/master/git/git-author-rewrite.sh

@Yunglow22
Copy link

940b22e6-fdf0-43ac-9e37-78558187b075-1_all_16

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