How to change the author of all your commits
Christian Vasquez

Christian Vasquez @chrisvasqm

About: Un simple mortal

Location:
Santo Domingo, Dominican Republic
Joined:
Feb 22, 2017

How to change the author of all your commits

Publish Date: Sep 28 '18
74 12

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.

Comments 12 total

  • Ben Halpern
    Ben HalpernSep 28, 2018

    Nice helpful post 😄

  • Rob Hoelz
    Rob HoelzSep 28, 2018

    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.

    • Chad Horohoe
      Chad HorohoeSep 29, 2018

      Far better solution!

    • John Bachir
      John BachirApr 16, 2021

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

  • Nick Taylor
    Nick TaylorSep 29, 2018

    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.

  • Brian Lampe
    Brian LampeSep 29, 2018

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

  • Multi
    MultiSep 29, 2018

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

  • Hendrik
    HendrikSep 30, 2018

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

  • m¡chael cook 🐌
    m¡chael cook 🐌Aug 24, 2019

    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
Add comment