How to fix author name/email in last Nth git commits
15, December 2021

Yesterday, I docker-compose exec into a docker container and wanted to see what author name/email is set in git config globally. So I ran this command:

git config user.email --global

For whatever reason, I didn't look at the output and forgot about it. After an hour, I made some commit and wanted to push them. By habit, I ran git log and noticed that commit author email is not my email. It's --global


What?!?

Then I remember what I ran earlier and realized that I misplaced --global flag in wrong place.

The flag should have been before user.email

git config --global user.email

Anything after user.email will be treated as value and will be set as email. Which exactly happened in my case.


So, I fixed my git author email by:

git config --global user.email my.email@mail.com


But what to do with the commits that are already made? How can I fix their author email?

After a google search, I found lots of approach to do similar things, but one stackoverflow comment caught my eyes. The suggested solution is:

git rebase --onto HEAD~{COMMIT_NUMBERS} --exec "git commit --amend --reset-author --no-edit" HEAD~{COMMIT_NUMBERS}

Above, {COMMIT_NUMBERS} is just a placeholder, you have replace it with how many of last commits you want to fix.


In my case, I needed to fix last 3 commits, so I ran this command:

git rebase --onto HEAD~3 --exec "git commit --amend --reset-author --no-edit" HEAD~3

It just loops over your last commits, and amend the commit with new author name/email.


Now git log shows my correct name/email.


If you only need to reset your latest one commit, this command does the same:

git commit --amend --reset-author --no-edit


Note: This command will reset commit hashes, So be careful if you've already pushed your commits to remote server because your team-mates may already have pulled your commits and made their own changes too.


Write comment about this article: