To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .
You can use interactive rebase. The answer from this post gives you an example: How to change the commit author for one specific commit?. The author asks for changing author at a specific commit, but interactive rebasing can be used to change authors of multiple commits if you edit all commits that you wish to change.
Rebase/amend seems inefficient, when you have the power of filter-branch at your fingertips:
git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
GIT_AUTHOR_EMAIL=correct@email;
GIT_AUTHOR_NAME="Correct Name";
GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all
(split across lines for clarity, but not necessary)
Be sure to inspect the result when you're done, to make sure that you didn't change anything you didn't mean to!
The interactive rebase approach is pretty nice when used in conjunction with exec. You can run any shell command against a specific commit or all commits in the rebase.
First set your git author settings
git config --global user.name "John Doe"
git config --global user.email [email protected]
Then to reset the author for all commits after the given SHA
git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"
This will pop up your editor to confirm the changes. All you need to do here is save and quit and it will go through each commit and run the command specified in the -x flag.
Per @Dave's comment below, you can also change the author while maintaining the original timestamps with:
git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <[email protected]>' -CHEAD"
To change the author only for the last commit:
git commit --amend --author 'Author Name <[email protected]>' --no-edit
Suppose you only want to change the author for the last N commits:
git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"
NOTES
--no-edit
flag makes sure the git commit --amend
doesn't ask an extra confirmationgit rebase -i
, you can manually select the commits where to change the author, the file you edit will look like this:
pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <[email protected]>' --no-edit
pick dc18f70 bugfix
This method was documented by GitHub for this very purpose (though GitHub has since removed it). The steps are:
git clone --bare https://github.com/user/repo.git
cd repo
OLD_EMAIL
, CORRECT_EMAIL
, and CORRECT_NAME
)#!/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
git push --force --tags origin 'refs/heads/*'
and you're done!If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With