How to Change the Commit Author for a Single Commit in Git
You committed with the wrong email address or name - maybe you forgot to set up Git on a new machine, or you committed from the wrong account. Git lets you correct the author information for individual commits.
TLDR: To change the author of the most recent commit, use git commit --amend --author="Name <[email protected]>". For an older commit, use git rebase -i HEAD~n, mark the commit as "edit", then run git commit --amend --author="Name <[email protected]>" followed by git rebase --continue.
In this guide, you'll learn how to fix author information in commits.
Prerequisites
You'll need Git installed on your system and commits with incorrect author information. Basic familiarity with Git commits and rebase will be helpful.
Understanding Author vs Committer
Git tracks two sets of information for each commit:
# View commit details
git log --format="%h %an <%ae> | %cn <%ce>" -1
# Output:
# abc123 Jane Developer <[email protected]> | John Smith <[email protected]>
# ^^^^^^^^^^^^^^^ Author ^^^^^^^^^^^^^^ Committer
- Author: Person who originally wrote the code
- Committer: Person who committed the code to the repository
Usually they're the same, but they can differ when applying patches or cherry-picking.
Changing the Most Recent Commit
To fix the author of your last commit:
# Change author of last commit
git commit --amend --author="Jane Developer <[email protected]>"
# Git opens editor - save and close to confirm
If you don't want to open an editor:
# Change author without editing message
git commit --amend --author="Jane Developer <[email protected]>" --no-edit
Using Environment Variables
You can also set author via environment variables:
# Set author for one commit
GIT_AUTHOR_NAME="Jane Developer" \
GIT_AUTHOR_EMAIL="[email protected]" \
git commit --amend --no-edit
This is useful in scripts.
Resetting to Current Git Config
To use your currently configured Git identity:
# First, check your current config
git config user.name
git config user.email
# Reset commit to use current config
git commit --amend --reset-author --no-edit
The --reset-author flag uses your configured name and email.
Changing an Older Commit
To change the author of a commit that is not the most recent:
# Start interactive rebase (go back 5 commits)
git rebase -i HEAD~5
Git opens an editor with your commits:
pick abc123 First commit
pick def456 Second commit (fix this one)
pick ghi789 Third commit
pick jkl012 Fourth commit
pick mno345 Fifth commit
Change pick to edit for the commit to modify:
pick abc123 First commit
edit def456 Second commit (fix this one)
pick ghi789 Third commit
pick jkl012 Fourth commit
pick mno345 Fifth commit
Save and close. Git stops at that commit:
# Git says: Stopped at def456
# Change the author
git commit --amend --author="Jane Developer <[email protected]>" --no-edit
# Continue the rebase
git rebase --continue
Finding the Commit to Change
If you don't know how far back the commit is:
# Search for commits by wrong author
git log --author="[email protected]" --oneline
# Or see all commits with author info
git log --format="%h %an <%ae>" --all
Once you find it, note the commit hash and count how many commits back it is.
Changing Author for Specific Commit by Hash
If you know the exact commit hash:
# Rebase to just before that commit
git rebase -i abc123^
# Mark that commit as 'edit'
# Then amend and continue as above
The ^ means "parent of this commit", which is where the rebase starts.
Batch Changing Author
To change the same author across multiple commits:
git rebase -i HEAD~10
# Mark all commits with wrong author as 'edit'
edit abc123 Commit 1
edit def456 Commit 2
pick ghi789 Commit 3 (correct author)
edit jkl012 Commit 4
Git stops at each marked commit:
# At each stop
git commit --amend --author="Correct Name <[email protected]>" --no-edit
git rebase --continue
Automating with exec
For many commits, use the exec command:
git rebase -i HEAD~20
# In the editor, add 'exec' commands
pick abc123 First commit
exec git commit --amend --author="Jane Developer <[email protected]>" --no-edit --allow-empty
pick def456 Second commit
exec git commit --amend --author="Jane Developer <[email protected]>" --no-edit --allow-empty
Or use a more advanced approach with filter-branch (see below).
Using Filter-Branch for Extensive Changes
For changing many commits throughout history:
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Jane Developer"
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
Warning: This rewrites all history. Make a backup first!
Using git-filter-repo (Modern Approach)
For safer bulk changes, use git-filter-repo:
# Install git-filter-repo first
# pip install git-filter-repo
# Create mailmap file
cat > mailmap << EOF
Jane Developer <[email protected]> <[email protected]>
EOF
# Apply changes
git filter-repo --mailmap mailmap
# Force push if needed
git push --force origin main
Verifying Changes
After changing author information:
# Check the commit
git show --format=fuller HEAD
# Output shows:
# Author: Jane Developer <[email protected]>
# AuthorDate: Mon Jan 15 14:30:00 2024
# Commit: Jane Developer <[email protected]>
# CommitDate: Mon Jan 15 14:30:00 2024
# View multiple commits
git log --format="%h %an <%ae>" -5
Changing Both Author and Committer
To set both at once:
# Change both author and committer
GIT_AUTHOR_NAME="Jane Developer" \
GIT_AUTHOR_EMAIL="[email protected]" \
GIT_COMMITTER_NAME="Jane Developer" \
GIT_COMMITTER_EMAIL="[email protected]" \
git commit --amend --no-edit
Pushing Changes
If you modified commits that were already pushed:
# Force push (coordinate with team first!)
git push --force origin feature-branch
# Safer: force with lease
git push --force-with-lease origin feature-branch
Warning: Only force push to branches you own or after coordinating with your team.
Preventing Wrong Author in Future
Set your Git identity globally:
# Set for all repositories
git config --global user.name "Jane Developer"
git config --global user.email "[email protected]"
# Verify settings
git config --global user.name
git config --global user.email
Or per repository:
# Set for current repository only
git config user.name "Jane Developer"
git config user.email "[email protected]"
Using Conditional Includes
For automatic identity switching:
# ~/.gitconfig
[user]
name = Jane Developer
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# ~/.gitconfig-work
[user]
name = Jane Developer
email = [email protected]
This automatically uses the right email based on directory.
Common Scenarios
Committed from wrong account:
# Just committed with wrong account
git commit --amend --reset-author --no-edit
Forgot to set Git config on new machine:
# Set config
git config user.name "Jane Developer"
git config user.email "[email protected]"
# Fix recent commits
git rebase -i HEAD~5
# Mark all as 'edit', then:
git commit --amend --reset-author --no-edit
git rebase --continue
Used work email for personal project:
git commit --amend --author="Jane Developer <[email protected]>" --no-edit
Handling Merge Commits
Changing author of merge commits requires special handling:
git rebase -i --rebase-merges HEAD~10
# Or use filter-branch for merge commits
git filter-branch --env-filter '...' -- --all
Best Practices
Always backup before rewriting history:
# Create backup branch
git branch backup-before-author-change
# Make changes
git rebase -i HEAD~10
# If something goes wrong
git reset --hard backup-before-author-change
Only change commits that have not been shared:
# Good: Local commits
git commit --amend --reset-author
# Risky: Published commits
git push --force
Communicate before force pushing:
# Tell team before force push
"About to fix author info on feature-x branch,
please don't push to it for 5 minutes"
Use mailmap for historical display:
# Create .mailmap file
cat > .mailmap << EOF
Correct Name <[email protected]> <[email protected]>
EOF
# Commit mailmap
git add .mailmap
git commit -m "Add mailmap for author corrections"
Mailmap changes how Git displays authors without rewriting history.
When Not to Change Author
Do not change author when:
- The commit is on main/master
- Others have pulled the branch
- It's part of a signed commit you don't control
- You're not certain about the correct attribution
In these cases, document the correct author in commit messages or use mailmap.
Now you know how to change the commit author for a single commit. Use git commit --amend --author for recent commits and git rebase -i for older ones. Remember to only modify commits that have not been shared, and always coordinate with your team before force pushing.
Found an issue?