Skip to content

Instantly share code, notes, and snippets.

@jraleman
Last active October 12, 2020 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jraleman/c27868f93d45fcc48e0a187521025301 to your computer and use it in GitHub Desktop.
Save jraleman/c27868f93d45fcc48e0a187521025301 to your computer and use it in GitHub Desktop.
Script to change git commits' author
#!/bin/sh
# Usage message if number of arguments are invalid.
if [ "$#" -ne 3 ] ; then
echo "usage: sh $0 wrong-email new-email new-name"
exit 1
fi
# Save args to variables.
WRONG_EMAIL="$1"
NEW_EMAIL="$2"
NEW_NAME="$3"
# Use a git filter-branch to batch script to fix your username and email values
# in one or multiple commits, within the branches.
# Source: https://www.git-tower.com/learn/git/faq/change-author-name-email
git filter-branch --env-filter '
WRONG_EMAIL='\'$WRONG_EMAIL\''
NEW_EMAIL='\'$NEW_EMAIL\''
NEW_NAME='\'$NEW_NAME\''
if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment