How to change the author of all your commits

Christian Vasquez - Sep 28 '18 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player