NeuroAgent

Complete Guide: Change Git Commit Author Info

Learn how to change author and committer names/emails for multiple Git commits using filter-repo, filter-branch, or interactive rebase. Complete guide with best practices and troubleshooting tips.

Question

How do I change the author and committer name/email for multiple commits in Git?

NeuroAgent

To change the author and committer name/email for multiple commits in Git, you can use either the modern git filter-repo command or the traditional git filter-branch method. The recommended approach is git filter-repo as it’s safer and more efficient, but filter-branch still works for repositories where filter-repo isn’t available.

Contents

Understanding Author vs Committer in Git

In Git, every commit has two distinct identities:

  • Author: The person who originally wrote the code
  • Committer: The person who last applied the commit to the repository

This distinction becomes important when commits are merged or cherry-picked between different branches or repositories. When changing author/committer information, you typically need to update both to maintain consistency.

The Stack Overflow discussion explains that these fields can differ in scenarios like:

  • When code is contributed via email
  • When commits are forwarded through maintainers
  • When working with shared or team repositories

Using git filter-repo (Recommended Method)

git filter-repo is the modern, preferred tool for rewriting Git history. It’s faster, safer, and less error-prone than the older filter-branch.

Basic Usage

bash
git filter-repo --commit-callback '
old_email = b"old.email@example.com"
new_email = b"new.email@example.com"
new_name = b"New Author Name"

if commit.author_email == old_email:
    commit.author_email = new_email
    commit.author_name = new_name
if commit.committer_email == old_email:
    commit.committer_email = new_email
    commit.committer_name = new_name
'

Email Replacement Script

For simple email replacements, you can use the --email-callback option:

bash
git filter-repo --email-callback '
return email if email != b"OLD_EMAIL" else b"NEW_EMAIL"
' --force

Combined Email and Name Changes

According to the GitHub gist, you can combine multiple filters:

bash
git filter-repo \
--email-callback ' return email if email != b"OLD_EMAIL" else b"NEW_EMAIL" ' \
--name-callback 'return name.replace(b"OLD_AUTHOR", b"NEW_AUTHOR")' \
--force \
--refs HEAD~<NUMBER_OF_COMMITS>..<BRANCH_NAME>

Installation

git filter-repo may not be installed by default. As noted in the SuperUser answer, you can install it with:

bash
pip install git-filter-repo

Using git filter-branch (Legacy Method)

git filter-branch is the older method that still works but is deprecated due to its complexity and potential for errors.

Basic Environment Filter

The most common approach uses --env-filter:

bash
git filter-branch -f --env-filter '
OLD_EMAIL="old.email@example.com"
CORRECT_NAME="New Author Name"
CORRECT_EMAIL="new.email@example.com"

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

One-Liner Version

For simpler cases, you can use the one-liner approach mentioned in the Stack Overflow answer:

bash
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='new@email'; GIT_COMMITTER_NAME='Newname'; GIT_COMMITTER_EMAIL='new@email';" HEAD...

Limitations

As noted in the DesignGurus answer, filter-branch has several drawbacks:

  • Performance: Slower, especially on large repositories
  • Risk of Errors: More prone to mistakes compared to git filter-repo
  • Deprecated: Not recommended for new projects
  • Complexity: Requires careful configuration to avoid issues

Interactive Rebase Approach

For a more controlled, manual approach, you can use interactive rebase to change author information commit by commit.

Step-by-Step Process

  1. Start an interactive rebase from the commit before the first one you want to change:
bash
git rebase -i HEAD~<NUMBER_OF_COMMITS>
  1. Edit the rebase script to change pick to reword or edit for each commit you want to modify

  2. For each commit marked with edit, amend the commit:

bash
git commit --amend --author="New Author Name <new.email@example.com>" --no-edit
  1. Continue the rebase:
bash
git rebase --continue

Automated Interactive Rebase

According to the DeployHQ documentation, you can automate this process by using exec commands in the rebase script:

pick bef03ed Revert "Add the correct link to Brie"
exec git commit --amend --reset-author -CHEAD
pick 74dd8b3 New folder
exec git commit --amend --reset-author -CHEAD
pick 56aedbe remove old folder
exec git commit --amend --reset-author -CHEAD

When to Use Interactive Rebase

The GeeksforGeeks guide suggests this approach is best when:

  • You want to be selective about which commits to change
  • You need to modify commit messages along with author information
  • You’re working with a small number of commits
  • You want to review each change individually

Best Practices and Considerations

Before Making Changes

  1. Create a Backup: Always backup your repository before rewriting history
  2. Check Repository Status: Ensure you’re on the correct branch and have no uncommitted changes
  3. Inform Team Members: If working collaboratively, notify others about the upcoming history rewrite

Repository Size Considerations

  • Small repositories: Interactive rebase or filter-repo work well
  • Large repositories: filter-repo is significantly faster than filter-branch
  • Shared repositories: Force push with caution and communicate changes to all collaborators

Branch and Tag Handling

When using filter-branch or filter-repo, be mindful of:

  • Branch references: The --branches option affects all local branches
  • Tags: Use --tag-name-filter cat to preserve tag relationships
  • Remote branches: These won’t be affected unless explicitly included

Performance Optimization

For large repositories with many commits:

  • Use --refs to limit the scope of changes
  • Consider running in a temporary directory to avoid disk space issues
  • Monitor memory usage during the process

Troubleshooting Common Issues

Merge Conflicts

If you encounter merge conflicts during rebase:

  1. Resolve conflicts manually
  2. Use git add to mark resolved files
  3. Continue with git rebase --continue

Permission Issues

If you encounter permission errors:

  1. Ensure you have write access to the repository
  2. Check file permissions
  3. Consider running with appropriate user privileges

Filter-Repo Not Found

If git filter-repo is not available:

  1. Install it using pip install git-filter-repo
  2. Alternatively, use filter-branch with caution
  3. Check if it’s available in your Git installation

History Rewrite Warnings

Git will warn you when rewriting shared history:

  • These warnings are normal but should be heeded
  • Consider creating a new branch if unsure
  • Always test changes on a local copy first

Incomplete Changes

If some commits aren’t modified as expected:

  1. Verify the email/name patterns match exactly
  2. Check the commit range specification
  3. Consider using more specific filtering criteria

Sources

  1. Stack Overflow - How do I change the author and committer name/email for multiple commits?
  2. Git Tower - How can I change the author (name / email) of a commit?
  3. DesignGurus.io - How do I change the author and committer name/email for multiple commits?
  4. GeeksforGeeks - How to Change Author and Committer Info for Multiple Git Commits?
  5. SuperUser - Rewrite and Resign git commit-history with git-filter-repo
  6. GitHub Gist - Change Git commit author / email in existing commits
  7. DeployHQ - How can I change the author of multiple Git commits?
  8. Git SCM Documentation - git-rebase

Conclusion

Changing author and committer information for multiple Git commits requires careful consideration and the right tools. The recommended approach is to use git filter-repo for its safety and performance, though filter-branch remains a viable alternative for legacy systems. For more controlled scenarios, interactive rebase provides manual oversight over each commit change.

Key recommendations:

  • Always backup your repository before rewriting history
  • Use git filter-repo when possible for better performance and safety
  • Communicate changes to team members when working collaboratively
  • Test changes on a local copy before applying to shared repositories
  • Be cautious with force pushes to avoid disrupting collaborators’ work

By following these methods and best practices, you can successfully update author and committer information across multiple commits while maintaining repository integrity and team collaboration.