How to change the commit author for one specific commit?
Interactive rebase off of a point earlier in the history than the commit you need to modify (git rebase -i <earliercommit>
). In the list of commits being rebased, change the text from pick
to edit
next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:
git commit --amend --author="Author Name <[email protected]>" --no-edit
For example, if your commit history is A-B-C-D-E-F
with F
as HEAD
, and you want to change the author of C
and D
, then you would...
- Specify
git rebase -i B
(here is an example of what you will see after executing thegit rebase -i B
command)- if you need to edit
A
, usegit rebase -i --root
- if you need to edit
- Change the lines for both
C
andD
frompick
toedit
- Exit the editor (for vim, this would be pressing Esc and then typing
:wq
). - Once the rebase started, it would first pause at
C
- You would
git commit --amend --author="Author Name <[email protected]>"
- Then
git rebase --continue
- It would pause again at
D
- Then you would
git commit --amend --author="Author Name <[email protected]>"
again git rebase --continue
- The rebase would complete.
- Use
git push -f
to update your origin with the updated commits.
If you just want to change the author of your last commit, you can do this:
Reset your email to the config globally:
git config --global user.email [email protected]
Now reset the author of your commit without edit required:
git commit --amend --reset-author --no-edit
Github documentation contains a script that replaces the committer info for all commits in a branch (now irretrievable, this is the last snapshot).
Run the following script from terminal after changing the variable values
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
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
Push the corrected history to GitHub:
git push --force --tags origin 'refs/heads/*'
OR if you like to push selected references of the branches then use
git push --force --tags origin 'refs/heads/develop'
The accepted answer to this question is a wonderfully clever use of interactive rebase, but it unfortunately exhibits conflicts if the commit we are trying to change the author of used to be on a branch which was subsequently merged in. More generally, it does not work when handling messy histories.
Since I am apprehensive about running scripts which depend on setting and unsetting environment variables to rewrite git history, I am writing a new answer based on this post which is similar to this answer but is more complete.
The following is tested and working, unlike the linked answer.
Assume for clarity of exposition that 03f482d6
is the commit whose author we are trying to replace, and 42627abe
is the commit with the new author.
Checkout the commit we are trying to modify.
git checkout 03f482d6
Make the author change.
git commit --amend --author "New Author Name <New Author Email>"
Now we have a new commit with hash assumed to be 42627abe
.
Checkout the original branch.
Replace the old commit with the new one locally.
git replace 03f482d6 42627abe
Rewrite all future commits based on the replacement.
git filter-branch -- --all
Remove the replacement for cleanliness.
git replace -d 03f482d6
Push the new history (only use --force if the below fails, and only after sanity checking with
git log
and/orgit diff
).git push --force-with-lease
Instead of 4-5 you can just rebase onto new commit:
git rebase -i 42627abe