Skip to content

Instantly share code, notes, and snippets.

@PrashantDhungana
Created May 9, 2025 13:02
Show Gist options
  • Save PrashantDhungana/ea5ab4e2ab7775e112ac4e357b175c7c to your computer and use it in GitHub Desktop.
Save PrashantDhungana/ea5ab4e2ab7775e112ac4e357b175c7c to your computer and use it in GitHub Desktop.
Bash Script to Update Git Commit History - Change Author and Committer Information
# This script updates author and committer information for past Git commits.
# It modifies commit history where the specified OLD_EMAIL is found,
# replacing it with the CORRECT_NAME and CORRECT_EMAIL.
# 🔥 Why Use This Script?
# - Correct misattributed commits after email address changes.
# - Update author information across all branches and tags.
# - Maintain consistency in commit history before sharing a project.
# 📦 How It Works:
# - Accepts OLD_EMAIL, CORRECT_NAME, and CORRECT_EMAIL as inputs.
# - Applies changes to both author and committer information.
# - Updates all branches and tags in the repository.
```bash
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="ABC"
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment