If you want to rewrite the author on previous commits you will also inadvertently change the commit date to the current day. Ideally, you want to keep the date of the original commits (this is useful if you want to make the history of git contributions accurate - lets say for the Github contributions in your user history overview). You should also be sure to only target commits with a specific author or email, so you don't re-write every single commit.
This is how you do that:
Step 1:
Set your email & name to on the local repo
git config user.name "Your name"
git config user.email "[email protected]"
add a --global
flag to set it everywhere.
Step 2: Rebase your commits and change the name & email to the current name & email in your config settings.
git rebase -r <commit-hash> \
--exec 'if [ "$(git log -n 1 --format="%ae")" = “[email protected]” ]; then git commit --amend --no-edit --reset-author --date="$(git log -n 1 --format=%aD)"; fi'
How it works:
By rebasing on a specific commit-hash
you are processing each commit starting from the commit you specified with the hash.
$(git log -n 1 --format="%ae")
retrieves the name of the author from the latest commit (the one being processed) using the git log command with the --format="%ae"
option, which specifies to display only the author email. You can change this part to --format="%an"
to target a author names.
The conditional statement [ "$(git log -n 1 --format="%ae")" = “[email protected]” ]
checks if the name of the author from the latest commit matches the specific email “[email protected]”.
If the condition is true, the subsequent git commit --amend --no-edit --reset-author --date="$(git log -n 1 --format=%aD)"
command will be executed as part of the --exec option during the rebase operation. Its important to add the --date=
flag with the matcher so that we don't change the original commit date during the rebase. This is selecting the most recent commit of the branch (which is the one you are currently processing) and using that commit's date thereby leaving the commit message with the same date.
But be careful, you are changing git history and you are changing the commit hashes for each node on the branch. You will have to force push the change up to a public repository (if you are changing a branch that has already been pushed up).
Using this command goes without saying - only change commit emails & author names if there has been some error in the email that was used for the commit, and you want to attribute the proper developer. You may have committed for someone else, or you may have forgot to set the proper name & email before starting your repo.