How can I change the author of multiple Git commits?
There are several reasons you might need to change the author on past commits: you switched email addresses, your Git config had the wrong name, or you committed from a shared machine that wasn't configured with your details. Whatever the reason, Git gives you a few ways to fix it.
A word of caution: all of these methods rewrite commit history. If you've already pushed the affected commits to a shared branch, you'll need to coordinate with your team before force pushing. Rewriting public history can cause problems for anyone who has based work on the original commits.
Using interactive rebase
This is the approach that gives you the most control. It works well when you need to change the author on a specific range of commits.
First, make sure your local Git config has the correct author details:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Then start an interactive rebase from the commit before the first one you want to change. For example, if you want to rewrite everything after commit 956951bf:
git rebase -i 956951bf -x "git commit --amend --reset-author -CHEAD"
Your editor will open with a list of commits. Each pick line is followed by an exec line that resets the author:
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
Review the list to confirm these are the commits you want to change. Save and close the editor (in nano, press ctrl+x then y then Enter). Git will replay each commit with your current author details.
Using git filter-repo for bulk changes
If you need to change the author across your entire repository history --- say, hundreds of commits --- git filter-repo is faster and more reliable than interactive rebase:
git filter-repo --commit-callback '
if commit.author_email == b"old@example.com":
commit.author_name = b"Your Name"
commit.author_email = b"new@example.com"
commit.committer_name = b"Your Name"
commit.committer_email = b"new@example.com"
'
This selectively updates only commits that match the old email, leaving everything else untouched. Install it with brew install git-filter-repo on macOS or pip3 install git-filter-repo on any platform.
Verifying the changes
After rewriting, confirm the author details look correct:
git log --format='%h %an <%ae>' -10
You should see your updated name and email on the affected commits. If everything looks right, force push to update the remote:
git push origin --force-with-lease
Using --force-with-lease instead of --force is safer --- it will refuse to push if someone else has pushed new commits since your last fetch.
Related
git configcommand reference --- setting user name and emailgit rebasecommand reference --- interactive rebase basicsgit pushcommand reference --- force pushing after history rewrites
DeployHQ deploys from your repository regardless of commit author details. Once your history is clean, connect your repo and set up automatic deployments.