How do I change the author for a range of commits?
NOTE: This answer changes SHA1s, so take care when using it on a branch that has already been pushed. If you only want to fix the spelling of a name or update an old email, Git lets you do this without rewriting history using .mailmap
. See my other answer.
First, if you haven't already done so, you will likely want to fix your name in git-config:
git config --global user.name "New Author Name"
git config --global user.email "<[email protected]>"
This is optional, but it will also make sure to reset the committer name, too, assuming that's what you need.
To rewrite metadata for a range of commits using a rebase, do
git rebase -r <some commit before all of your bad commits> \
--exec 'git commit --amend --no-edit --reset-author'
--exec
will run the git commit
step after each commit is rewritten (as if you ran git commit && git rebase --continue
repeatedly).
If you also want to change your first commit (also called the 'root' commit), you will have to add --root
to the rebase call.
This will change both the committer and the author to your user.name
/user.email
configuration. If you did not want to change that config, you can use --author "New Author Name <[email protected]>"
instead of --reset-author
. Note that doing so will not update the committer -- just the author.
If you just want to change the most recent commit, a rebase is not necessary. Just amend the commit:
git commit --amend --no-edit --reset-author
-r,--rebase-merges
may not exist for you. As a replacement, you can use -p
. Note that -p
has serious issues and is now deprecated.
This answer uses
git-filter-branch
, for which the docs now give this warning:git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance). These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended. Please use an alternative history filtering tool such as git filter-repo. If you still need to use git filter-branch, please carefully read SAFETY (and PERFORMANCE) to learn about the land mines of filter-branch, and then vigilantly avoid as many of the hazards listed there as reasonably possible.
Changing the author (or committer) would require rewriting all of the history. If you're okay with that and think it's worth it then you should check out git filter-branch. The manual page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. -- see the "Environment Variables" section of the git manual page.
Specifically, you can fix all the wrong author names and emails for all branches and tags with this command (source: GitHub help):
#!/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
For using alternative history filtering tool git filter-repo, you can first install it and construct a git-mailmap
according to the format of gitmailmap.
Proper Name <[email protected]> Commit Name <[email protected]>
And then run filter-repo with the created mailmap:
git filter-repo --mailmap git-mailmap
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