Skip to content
Bite-Sized Musings

TIL: How to Change Author Name or Email in git

git, rebase, git-author, github, code snippet, today-i-learned1 min read

Context:

So I just realized today that all my commits were made with my previous' company's email. Since I'm in a new place now, figured I should change author email for git.

But I wanted to keep my email private, and luckily GitHub allows that with their noreply feature. Anyway, I realized that all my commits, all the way to the first commit in this project was made with the wrong email. I didn't want to change the actual commit dates, so I looked for a way to change the author name and email while keeping the original commit timestamps.

Change your git email

  1. Get your noreply github email. Look for Keep my email addresses private option. The email would look like this: {ID}+{username}@users.noreply.github.com.
1git config --global user.email "{ID}+{username}@users.noreply.github.com"

SO Answer

Changing the details of your last commit

If it's just the last commit you wanted to ammend, use this:

1git commit --amend --reset-author

Note that this will update the timestamp of the commit.

Changing the details for several commits

  1. If you'd like to change from a specific commit to the current HEAD, then first find the commit's SHA with this:
1git log
  1. Look for whichever point you'd like to start with. Then copy the SHA in this command:
1git rebase -i <YOUR_SHA> -x "git commit --amend --reset-author -CHEAD"

Note that this will update the commit timestamps.

  1. If you'd like to keep the timestamps intact, do this instead:
1git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <{ID}+{username}@users.noreply.github.com>' -CHEAD"

Note that you have to keep the <> and only replace the name and email.

SO Answer

  1. There's also an alternative way, which I haven't tried, but will consider:
1git rebase -i YOUR_SHA -x 'git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"'

Found it from a SO comment


Read more:

© 2022 by Bite-Sized Musings. All rights reserved.
Theme by LekoArts