Skip to main content

Command Palette

Search for a command to run...

Changing the author on previous git commits

Updated
2 min read
Changing the author on previous git commits

Problem

I started using a git repository on a laptop where I’ve not used it before. The repository is hosted on github and I choose not to expose my personal email address on github. Instead, I use my github-provided email address for authoring commits. However, I’d forgotten to set that up for this laptop, and accidentally committed to the repository using my personal email address.

Happily, github warned me about this when I tried to push my commits. This left me wanting to change the author of these commits so I could push them without exposing my email address.

Solution

Note: I used the windows command line, so syntax may differ in other terminals.

First I set the default author for the laptop:

git config --global user.name "Tim"
git config --global user.email "redacted@email.example"

I then changed the author on the last few commits. In my case there were 3, but you can change the number 3 to however many is relevant for you. This changes the author of the commits so they use the default author, configured above, but leaves the commit content and message the same.

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

Note that this will change the commit hashes, so it should only be done for commits which haven’t been pushed anywhere else.

Further Reading

I leaned heavily on this StackOverflow question: How do I change the author and committer name/email for multiple commits?

More from this blog

T

Tim talks tech

76 posts