Skip to content

Instantly share code, notes, and snippets.

@a-magdy
Last active August 23, 2023 12:31
Show Gist options
  • Save a-magdy/000db18d7dea340bef260c5a3c285c1f to your computer and use it in GitHub Desktop.
Save a-magdy/000db18d7dea340bef260c5a3c285c1f to your computer and use it in GitHub Desktop.
Change commits author
#!/bin/bash
function unique_list_of_committers() {
git log --format="%aN <%aE>" | sort -u
}
function change_author() {
FROM_EMAIL=$1
FROM_NAME=$2
TO_EMAIL=$3
TO_NAME=$4
if [ -z "$FROM_EMAIL" ] || [ -z "$FROM_NAME" ] || [ -z "$TO_EMAIL" ] || [ -z "$TO_NAME" ]; then
echo "usage: $0 <from-email> <from-name> <to-email> <to-name>"
exit 1
fi
if [ "$FROM_EMAIL" == "$TO_EMAIL" ] && [ "$FROM_NAME" == "$TO_NAME" ]; then
echo "usage: $0 <from-email> <from-name> <to-email> <to-name>"
echo "from and to emails and names are the same"
exit 1
fi
if [[ ! "$FROM_EMAIL" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "usage: $0 <from-email> <from-name> <to-email> <to-name>"
echo "from email is not valid"
exit 1
fi
if [[ ! "$TO_EMAIL" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "usage: $0 <from-email> <from-name> <to-email> <to-name>"
echo "to email is not valid"
exit 1
fi
git filter-branch -f --env-filter "GIT_COMMITTER_NAME='$FROM_NAME'; GIT_COMMITTER_EMAIL='$FROM_EMAIL'; GIT_AUTHOR_NAME='$TO_NAME'; GIT_AUTHOR_EMAIL='$TO_EMAIL';" HEAD;
}
printf 'listing unique committers before change:\n\n'
unique_list_of_committers
printf '\nchanging author..\n\n'
change_author "$@"
printf '\nlisting unique committers after change:\n\n';
unique_list_of_committers
# example usage:
# change-author.sh "[email protected]" "to be changed" "[email protected]" "change to"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment