Skip to content
πŸ‘‰All tips here

Git Conditional Configuration: Juggle Multiple Accounts

set git conditionally front-end tips

So, you feel super productive and push your changes to GitHub. Wow, great! You rock!
But, then, you suddenly realize you’ve pushed to your work repository using your personal account. Daaamn, it is so frustrating, I can feel the pain! 😩

If you’re like me, juggling multiple GitHub accounts (one for work, one for personal projects), you’ve probably been there too. It’s happened to me more times than I’d like to admit! But no worries, I want to share a game-changing solution using Git conditional configuration.

Git and the Conditional Includes

Git has a feature called conditional includes. It lets you set different configurations based on your current working directory. Believe it or not, it’s super easy to set up and will save you many headaches!

Here’s how you can set it up:

1. First, set up your main Git config file. This is usually in your home directory:

# ~/.gitconfig
[user]
name = Your Name
email = your.personal@email.com

[includeIf "gitdir/i:~/work/"]
path = ~/work/.gitconfig

If you are still not sure what or where is the Git config file that I am talking about, to find or create this file on macOS or Linux: open the terminal and run:

nano ~/.gitconfig

This will open the file in the nano text editor. And, if you are on Windows, then: open the command prompt and run:

notepad %USERPROFILE%\.gitconfig

2. Now, create a separate config file for your work projects:

# ~/work/.gitconfig
[user]
email = your.work@email.com

That’s it! Now, whenever you’re in a Git repo inside your ~/work/ directory, Git will automatically use your work email. Easy!

Double-Check Your Setup

Just in case, make sure everything’s working as it should. Navigate to one of your personal projects, here’s a quick way to check:

cd ~/projects/personal-site
git config --get user.email
# Should output: your.personal@email.com

Now, check a work project:

cd ~/work/awesome-project
git config --get user.email
# Should output: your.work@email.com

If you’re seeing the right email for each project, well then, my friend, you’ve just leveled up your Git game! πŸŽ‰

But Wait, There’s More!

If you want to get even fancier (and who doesn’t?), you can set up your config based on the remote URL. This is super handy if your work projects are all under a specific GitHub organization. Here’s how you can do it:

# ~/.config/git/config
[user]
name = Your Name
email = your.personal@email.com

[includeIf "hasconfig:remote.*.url:git@github.com:WorkOrg/**"]
path = ~/.config/git/work-config

# ~/.config/git/work-config
[user]
email = your.work@email.com

Now, any repo that has a remote URL matching git@github.com:WorkOrg/** will automatically use your work email. Pretty cool, huh?

Troubleshooting Your Git Conditional Configuration

If things aren’t working quite right, run this command. It will show you where each config setting is coming from.

git config --list --show-origin

It’s like X-ray vision for your Git config!

That’s it, enjoy because now you are on the safe side and ready to rock harder! πŸ™‚


Learn more from the Git docs here: https://git-scm.com/docs/git-config#_conditional_includes

Or check this another tip I wrote about Git here: https://front-end.tips/git-uncommit-a-quick-tip-for-undoing-commits/