Git Commit Authors: How to Find, Change, and Configure Commit Identities
Every Git commit records two distinct identities: the Author (who originally wrote the code) and the Committer (who applied the changes to the branch). While these are usually the same person, they can diverge during operations like rebasing, cherry-picking, or merging.
Whether you need to trace who wrote a line of code or fix a typo in your own commit email, this guide covers how to find, change, and automatically configure Git commit author details.
How to Find Commit Authors and Emails
1. One-Liner Commands
If you need the name and email for the most recent commit or a specific commit hash:
- For the latest commit:
git log -1 --format="%an <%ae>" - For a specific commit hash:
git show -s --format="%an <%ae>" <commit-hash>
2. Custom Formatting with git log
Standard git log output is verbose. You can customize the log layout using format placeholders:
%an: Author Name%ae: Author Email%cn: Committer Name%ce: Committer Email
Show the last 5 commits with just the hash, author name, email, and subject:
git log -5 --pretty=format:"%h - %an <%ae> : %s"
To filter commits by a specific author:
git log --author="John Doe"
3. Line-by-Line Inspection with git blame
To see who modified specific lines in a file, run git blame. Use the -e flag to show email addresses instead of usernames:
git blame -e path/to/your/file.py
4. Raw Commit Object Inspection
To view the raw commit metadata stored on disk by Git, bypass standard formatting with cat-file:
git cat-file -p <commit-hash>
How to Change Commit Authors and Emails
Rewriting commit authors changes their parent-child cryptographic link. This means changing an author changes the commit hash, replacing old commits with brand-new ones.
1. Modify the Most Recent Commit
If you just committed changes and realized you used the wrong email:
- Specify name and email manually:
git commit --amend --author="New Name <newemail@example.com>" --no-edit - Reset to match your current git config:
git commit --amend --reset-author --no-edit
2. Modify Specific Past Commits (Interactive Rebase)
If the bad commit is further back in history:
- Identify how many commits back the target commit is (e.g., 5 commits):
git rebase -i HEAD~5 - In the editor that opens, change the word
picktoedit(ore) next to the commit you want to modify. Save and close the editor. - Once Git pauses at that commit, run:
git commit --amend --author="Correct Name <email@example.com>" --no-edit - Resume the rebase process:
git rebase --continue
3. Mass Update the Entire Repository
If you need to replace an old or incorrect email across your entire commit history, use git-filter-repo (the modern, safe replacement for git filter-branch).
- Create a text file named
my-mailmapmapping the old email to the new one:Correct Name <correct@email.com> <wrong@email.com> - Run the filter command:
git filter-repo --mailmap my-mailmap
Alternative: Legacy Shell Script (filter-branch)
If you cannot install external tools, use the built-in shell script filter. Note that this is slower and deprecated:
git filter-branch --env-filter '
OLD_EMAIL="wrong@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="correct@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
The Golden Rule: Pushing rewritten history
Because these operations rewrite commit hashes, you must force push to the remote server:
git push --force-with-lease
Warning: Never force push to a branch that other developers are actively collaborating on without coordinating with them first.
Preventing Mistakes: Conditional Configs
You can configure Git to automatically swap your author email based on the directory path of the repository (e.g., work repositories vs. personal projects).
In your global ~/.gitconfig file, set up conditional includes:
# Default settings
[user]
name = Serhii Hrekov
email = personal@example.com
# Apply work settings for projects inside ~/work/
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
Then create a file named ~/.gitconfig-work with your work profile:
[user]
email = work@example.com
Sources
- [1] Git Book: Viewing the Commit History
- [2] StackOverflow Reference: How to see the original author of a commit
- [3] Git SCM Documentation: git-blame manual page
- [4] Git Book: Rewriting Git History
