DEV Community

yukaty
yukaty

Posted on

Clean Up Your GitHub using git-filter-repo: Email Updates and Commit Messages

Do you know that GitHub only tracks commits from verified email addresses? I discovered that after noticing gaps in my own contribution history. My mistake? Using different email addresses for my commits, including dummy emails for spam prevention.

In this article, I'll show you how to safely rewrite your git history to update email addresses and standardize commit messages. Your original timestamps and actual changes will remain exactly as they were.

Table of Contents:

Understanding GitHub Email Verification

GitHub provides you with official no-reply addresses. These addresses serve two purposes:

  • They keep your real email private
  • They ensure all your commits are properly tracked

You can set this up by following the official guide.

Fixing Historical Commits

Now, what about your existing commits with incorrect emails? You can actually rewrite your git history to update old email addresses while preserving timestamps and changes.

Step 1: Install git-filter-repo

pip install git-filter-repo
Enter fullscreen mode Exit fullscreen mode

This tool is not only faster but also safer than git filter-branch. It's specifically designed to handle repository history rewriting with better safety guarantees and performance. More details here.

Step 2: Clone Your Repository

git clone git@github.com:yourusername/yourrepo.git
cd yourrepo
Enter fullscreen mode Exit fullscreen mode

Step 3: Rewrite History

git filter-repo --force --email-callback '
old_email = b"your-old-email@example.com"
new_email = b"your-new-noreply@users.noreply.github.com"

if email == old_email:
    return new_email
return email
'
Enter fullscreen mode Exit fullscreen mode
  • The --email-callback option modifies only the email addresses in commits.

Step 4: Fix Remote Settings After git filter-repo

Since git filter-repo removes remote settings, re-add the remote repository.

git remote add origin git@github.com:yourusername/yourrepo.git
Enter fullscreen mode Exit fullscreen mode

Step 5: Review and Push Changes

git log --pretty=full  # Verify the changes
git push --force origin main
git push --force --tags
Enter fullscreen mode Exit fullscreen mode

Standardizing Commit Messages

Need to clean up your commit messages? git-filter-repo can help you convert to conventional commits, fix typos, or add ticket numbers. Here are some examples.

Basic Message Updates

git filter-repo --force --message-callback '
if b"Added" in message:
    return message.replace(b"Added", b"feat: add")
return message
'
Enter fullscreen mode Exit fullscreen mode

Adding Ticket Numbers

git filter-repo --force --message-callback '
if not b"JIRA-" in message:
    return b"JIRA-123: " + message
return message
'
Enter fullscreen mode Exit fullscreen mode

Multiple Pattern Replacements

git filter-repo --force --message-callback '
replacements = {
    b"Added": b"feat: add",
    b"Fixed": b"fix:",
    b"Updated": b"chore: update"
}
for old, new in replacements.items():
    if message.startswith(old):
        return message.replace(old, new)
return message
'
Enter fullscreen mode Exit fullscreen mode

Tips

Before making any changes:

  • Create a backup of your repository first
  • Consider using --dry-run first to preview changes
  • Verify your changes with git log --pretty=full before pushing

Hope this helps you clean up your GitHub history. Your future self will thank you!

Top comments (0)