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 "email@example.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 "firstname.lastname@example.org" # work repos git config user.name "John Doe" git config user.email "email@example.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!
It is a bit involved with the
config files, but it's a one-time thing. Create three files.
### ~/.gitconfig.personal [user] name = John Doe email = firstname.lastname@example.org signingkey = JDPERSONAL
[user] name = John Doe email = email@example.com signingkey = JDPROFESSIONAL
[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!