I recently made the mistake of committing to my GitHub account from my work laptop. This was all well and good until I noticed the commit log. Because my work laptop’s Git configuration had a different email address, GitHub was missing my avatar and was using an unlinked email address (my work email). To my horror GitHub was presuming me a different person!
Now being the kind of person that couldn’t just let this go, I had no choice but to revert the last 10 commits and wait till I got home that day to commit them from my personal laptop.
To make sure that I would never have to revert commits for the same reason again, I did a quick Google search and found a foolproof method.
So… this is how to change your Git configuration per project.
Global Git configuration
Since it’s my work laptop, my global Git configuration contains my work email address.
In case you haven’t already setup your global Git config, type the following commands into a terminal window, making sure to change the details to your own.
git config --global user.name JakeCobley git config --global user.email email@example.com
Project specific Git configuration
Now to set a project specific Git configuration. Open a terminal window from the root of the project and type the below, again changing the details to your own.
git config user.email firstname.lastname@example.org
And that’s it. You will now be committing to that project with the project specific email address and not the global Git configuration value.
I could have just added both email addresses to my GitHub account and not worried about any of the above or reverting commits. Personally, I didn’t want my work email linked to my GitHub account as it’s not an email address I have any control over and might not even have in a couple of years, so I found the above a cleaner, more future proof method.