DEV Community

Cover image for How to change the author of all your commits
Christian Vasquez
Christian Vasquez

Posted on • Edited on

How to change the author of all your commits

As a wise man once said:

"With great power comes great responsibility" - Benjamin Parker character from the Spider-man comics.

This should not be a stranger when dealing with Git, specially when rewriting your commit history.

Anytime you encounter a --force when looking for answers on how to do something with Git you should be really careful and read all the details about it before pushing changes to master or any other branch that is being worked in collaboration with others.

But this time I had a specific need, I had been working on a side project that had around 10 commits already and while I was checking what I had done with the git log command, I soon realized that the author had the same username but different emails. I had a mix of commits coming from my work and personal emails.

I don't think this would cause any real problem since I was just making a prototype, but I also noticed that my profile's contributions graph was not displaying any information about those commits because of my email issue.

This led me to think about the most rational solution, saying:

"Oh crap!".

Followed by a Google search about how can we do this, which led me to this StackOverflow page that had a comment by Tarandeep Singh that did just what I needed.

Which is:

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='yourname'; GIT_AUTHOR_EMAIL='youremail@example.com'; GIT_COMMITTER_NAME='yourname'; GIT_COMMITTER_EMAIL='youremail@example.com';" HEAD;
Enter fullscreen mode Exit fullscreen mode
**Note: make sure to change the placeholders to what you need them to be.**

And after a quick double check using git log then I would have to run:

git push --force origin master
Enter fullscreen mode Exit fullscreen mode

This would make sure to rewrite all my remote's master branch commits to display my personal email and username.

Bear in mind, you should be really careful when using git push --force, I did it because I know I'm the only one working on that project, but using this command in other situations might cause other problems.

Hopefully this can help others when they find themselves in this sort of situation :)

Alternative

Thanks to Rob for sharing this alternative down in the comments!

Great tip! As an alternative, if you're uncomfortable rewriting all of a project's history using git-filter-branch, Git has a feature known as mailmaps which allow you to canonicalize username and e-mail addresses.

Top comments (11)

Collapse
 
hoelzro profile image
Rob Hoelz

Great tip! As an alternative, if you're uncomfortable rewriting all of a project's history using git-filter-branch, Git has a feature known as mailmaps which allow you to canonicalize username and e-mail addresses.

Collapse
 
chadasapp profile image
Chad Horohoe

Far better solution!

Collapse
 
johnjoseph profile image
John Bachir

Thanks for this! mailmap doesn't come up in google searches as much as one might expect, it's a perfect solution for me

Collapse
 
waxrat profile image
mΒ‘chael cook 🐌 • Edited

Here's an example of how to fix the email address of commits.
Don't rewrite all commit authors, only the commits with the incorrect email address.

git filter-branch --env-filter '
   if [ "$GIT_AUTHOR_EMAIL" = root@localhost ]; then
     GIT_AUTHOR_EMAIL=michael@example.com
   fi
 ' HEAD
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nickytonline profile image
Nick Taylor • Edited

Helpful post. Thanks for sharing Christian.

We rebase at work which means we need to force a push. However, you can use a slightly safer force command.

Having said that, I renamed that alias a while back to pf. More aliases here.

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

That sounds awesome! I'll give it a try!

Collapse
 
themulti0 profile image
Multi

Thanks! Very helpful. Thanks for sharing. I will definitely use that whenever I encounter a problem like that.

Collapse
 
ld00d profile image
Brian Lampe

Thank you for this post. I had the exact same situation. In my case, most of the commits were under my work email.

Collapse
 
hoverbaum profile image
Hendrik

Sweet and concise post, thanks a lot. This is a problem I have run into way more than I would like to admit πŸ™ˆ

Collapse
 
ben profile image
Ben Halpern

Nice helpful post πŸ˜„