I just started working and was asked to create a new GitHub account for my work-related tasks. But there was one major issue when it came to setting up git on my machine. How do I handle personal commits and work-related commits?
General git setup
git config --global user.name "John Doe"
git config --global user.email "john.doe@gmail.com"
Issues with this approach
Change the global config every time before you commit to a different account.
Create repository-specific configurations.
# personal repos
git config user.name "John Doe"
git config user.email "john.doe@gmail.com"
# work repos
git config user.name "John Doe"
git config user.email "john.doe@company.com"
But this is a huge inconvenience. You might accidentally commit to your work repository using your personal credentials! Or every time you create a git repo for personal use, you'll have to set up the config. Ugh! Luckily, there's an easy way out!
Solution
It is a bit involved with the config
files, but it's a one-time thing. Create three files.
~/.gitconfig.personal
### ~/.gitconfig.personal
[user]
name = John Doe
email = john.doe@gmail.com
signingkey = JDPERSONAL
~/.gitconfig.company
[user]
name = John Doe
email = john.doe@company.com
signingkey = JDPROFESSIONAL
~/.gitconfig
[includeIf "gitdir:~/company/**/.git"]
path = ~/.gitconfig.company
[include]
path = ~/.gitconfig.personal
Now all your commits inside the ~/company
directory will use your company credentials, and all other commits will use your personal credentials!