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:
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='yourname'; GIT_AUTHOR_EMAILemail@example.com'; GIT_COMMITTER_NAME='yourname'; GIT_COMMITTER_EMAILfirstname.lastname@example.org';" 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!
I’ve sent a message to my family and delegated my open source projects to my friends. With my last tweet sent, I turn off my laptop, phone, and tablet. My Digital Sabbath begins in 10 minutes: no digital devices for the next month.