When working with Git, there may be instances where you need to change the author of a single commit. This could happen if the wrong credentials were used, or if you need to attribute the commit to a different contributor. Git provides tools to update commit authorship, even for commits that have already been made.
Here’s a step-by-step guide to changing the commit author for a single commit.
Understanding Git Commit Author Information
Every Git commit stores the following:
- Author Name and Email: The person who wrote the code.
- Committer Name and Email: The person who committed the code (often the same as the author).
To check the author and committer details for a specific commit, run:
git show <commit-hash>
Changing the Author for the Latest Commit
If the commit you want to modify is the most recent one, you can use the --amend
option.
Steps:
- Update your Git author configuration:
git config user.name “New Author Name”
git config user.email “[email protected]”
2. Amend the commit with the new author:
git commit –amend –reset-author –no-edit
3. Push the updated commit (use --force
since history is being rewritten):
git push –force
Key Points:
- The
--reset-author
flag sets the commit’s author to the current global or local Git user. - The
--no-edit
flag prevents changes to the commit message.
Changing the Author for an Older Commit
If the commit you want to update is not the latest one, you can use an interactive rebase.
Steps:
- Start an interactive rebase:
git rebase -i <commit-before-target-commit>
Replace <commit-before-target-commit>
with the hash of the commit preceding the one you want to modify. For example, to edit the second commit in history, provide the hash of the first commit.
2. Mark the target commit for editing: The interactive rebase editor will display a list of commits. Change the word pick
to edit
for the commit you want to modify.
Example:
pick abc123 First commit
edit def456 Commit to edit
pick ghi789 Another commit
3. Update the author: After the rebase process stops at the target commit, change the author details: