How do I change the author and committer name/email for multiple commits in Git?
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
- Using git filter-repo (Recommended Method)
- Using git filter-branch (Legacy Method)
- Interactive Rebase Approach
- Best Practices and Considerations
- Troubleshooting Common Issues
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
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:
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:
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:
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:
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:
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
- Start an interactive rebase from the commit before the first one you want to change:
git rebase -i HEAD~<NUMBER_OF_COMMITS>
-
Edit the rebase script to change
picktorewordoreditfor each commit you want to modify -
For each commit marked with
edit, amend the commit:
git commit --amend --author="New Author Name <new.email@example.com>" --no-edit
- Continue the rebase:
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
- Create a Backup: Always backup your repository before rewriting history
- Check Repository Status: Ensure you’re on the correct branch and have no uncommitted changes
- Inform Team Members: If working collaboratively, notify others about the upcoming history rewrite
Repository Size Considerations
- Small repositories: Interactive rebase or
filter-repowork well - Large repositories:
filter-repois significantly faster thanfilter-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
--branchesoption affects all local branches - Tags: Use
--tag-name-filter catto preserve tag relationships - Remote branches: These won’t be affected unless explicitly included
Performance Optimization
For large repositories with many commits:
- Use
--refsto 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:
- Resolve conflicts manually
- Use
git addto mark resolved files - Continue with
git rebase --continue
Permission Issues
If you encounter permission errors:
- Ensure you have write access to the repository
- Check file permissions
- Consider running with appropriate user privileges
Filter-Repo Not Found
If git filter-repo is not available:
- Install it using
pip install git-filter-repo - Alternatively, use
filter-branchwith caution - 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:
- Verify the email/name patterns match exactly
- Check the commit range specification
- Consider using more specific filtering criteria
Sources
- Stack Overflow - How do I change the author and committer name/email for multiple commits?
- Git Tower - How can I change the author (name / email) of a commit?
- DesignGurus.io - How do I change the author and committer name/email for multiple commits?
- GeeksforGeeks - How to Change Author and Committer Info for Multiple Git Commits?
- SuperUser - Rewrite and Resign git commit-history with git-filter-repo
- GitHub Gist - Change Git commit author / email in existing commits
- DeployHQ - How can I change the author of multiple Git commits?
- 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-repowhen 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.