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:
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.
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='yourname'; GIT_AUTHOR_EMAILfirstname.lastname@example.org'; GIT_COMMITTER_NAME='yourname'; GIT_COMMITTER_EMAILemail@example.com';" HEAD;
And after a quick double check using
git log then I would have to run:
git push --force origin master
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 :)
Thanks to Rob for sharing this alternative down in the comments!
Top comments (12)
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.
Far better solution!
Thanks for this! mailmap doesn't come up in google searches as much as one might expect, it's a perfect solution for me
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.
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.
My Git Aliases
That sounds awesome! I'll give it a try!
Thanks! Very helpful. Thanks for sharing. I will definitely use that whenever I encounter a problem like that.
Thank you for this post. I had the exact same situation. In my case, most of the commits were under my work email.
Nice helpful post 😄
Sweet and concise post, thanks a lot. This is a problem I have run into way more than I would like to admit 🙈
Mercurial uses the Convert extension for that.