Skip to content

Instantly share code, notes, and snippets.

@nelup20
Last active February 9, 2024 12:00
Show Gist options
  • Save nelup20/4bd105171f826a4e52e0d7b09a02be49 to your computer and use it in GitHub Desktop.
Save nelup20/4bd105171f826a4e52e0d7b09a02be49 to your computer and use it in GitHub Desktop.
Shell script to change the author email in past commits to a new email (so you don't accidentally change ALL emails in case there are multiple authors). Force pushes to main/master branch. Use at your own risk!
#!/usr/bin/env bash
if ! [ "$(command -v git-filter-repo)" ]; then
echo 'Error: git-filter-repo is not installed. If you have pip: "pip install git-filter-repo"';
exit 1;
fi
# HTTPS or SSH
repo_url=$1;
git clone $repo_url;
repo_dir=$(basename $repo_url | head -c -5);
cd $repo_dir;
# Replace the author email in all commits with the new email
git-filter-repo --email-callback '
return email if email != b"yourold@email" else b"yournew@email"
'
# Commit (original commit date won't be modified)
git filter-branch --commit-filter 'git commit-tree -S "$@";';
# Force push to branch (overwrites main/master)
git remote add origin $repo_url;
branch_name=$(git rev-parse --abbrev-ref HEAD)
git push --set-upstream origin $branch_name -f;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment