Manage Separate Git Configurations Per Folder on Linux
Since I use the same computer for both personal and professional work, I need to separate my Git configuration. I use GitHub for my personal projects and GitLab for professional ones, so keeping things clean is essential.
This guide explains how to manage two Git environments on a single machine, so you no longer have to worry about which configuration is being used depending on the folder you’re working in. No more pushing to production with your personal account by mistake.
I’m assuming you’re working on a Linux environment.
Update your global .gitconfig
First, open (or create) your global Git configuration file:
nano ~/.gitconfig
Then add the following content:
[includeIf "gitdir:~/projects/professional/"]
path = ~/.gitconfig-professional
[includeIf "gitdir:~/projects/personal/"]
path = ~/.gitconfig-personal
You can change the parent folders to match your own structure. In this example, I’m using ~/projects/professional and ~/projects/personal, but you can name them however you like.
With this configuration:
- Everything under
~/projects/professionalwill use the professional Git configuration. - Everything under
~/projects/personalwill use the personal Git configuration.
Create the professional configuration
Now create the professional config file:
nano ~/.gitconfig-professional
Add:
[user]
name = FirstName LastName
email = your_pro_email@domain.com
Create the personal configuration
Then create the personal config file:
nano ~/.gitconfig-personal
Add:
[user]
name = FirstName LastName
email = your_personal_email@domain.com
That’s it
Git will automatically return the correct email address based on the directory you’re in:
cd ~/projects/personal
git config user.email # -> Personal email
cd ~/projects/professional
git config user.email # -> Professional email
No more confusion. No more accidental pushes with the wrong account.
You can fully customize each environment to suit your needs and even add additional configurations if required.
For more details, you can refer to the Official Git Configuration documentation. However, it can be quite verbose, so searching directly on Google for specific settings might save you time.