loading...

Multiple gitconfig

jma profile image Jie Originally published at jma.im ・1 min read

I need to do dev of work on my personal laptop. Usually, I will clone the repository and set a work email for that repository by git config user.email <your@email.com>. That requests config for each working repositories, and if there are other settings, it requires more actions. And most important, I forget to set up it sometimes.

Later, I did search on the internet and found I am actually able to set a separated gitconfig for the specific folder. Below are the steps.

Create a separated gitconfig file

~/.gitconfig-work

Add the settings you need into the file, like email/name

git config -f ~/.gitconfig-work user.email <your@email.com>

Now we have gitconfig file for working.

Set work folder to use the new config file

git config --global "includeIf.gitdir:~/work/.path" "~/.gitconfig-work"

Then for all repositories inside of work folder, it will use the new config file.

Bouns

Add isWork to the new config file

git config -f ~/.gitconfig-work core.isWork true

And run below command inside of the work folder to see if the config is applied

git config --get core.isWork || echo false

credit/reference: https://filipe.kiss.ink/multiple-gpg-keys-git/

Discussion

pic
Editor guide
 

Thanks for sharing.

I am interested for this to use a different email for my work repos. Is there a way I can keep my settings my mine gitconfig (such as aliases) and just override the email portion in a second git config for work?

I linked back to your post in mine on git configs.

dev.to/michaelcurrin/dotfiles-git-...

 

I think it only uses the settings in the gitconfig-work to override same one in the gitconfig, it is only the email and name by my example, so all your other settings like alias should still work as before

 

Perfect, thanks

 

This is so useful now that more and more people are working from home often on their personal machines. Nothing like making a commit to a work project with your personal email (or worse putting your work mail on a public project :))

You can also use

git config user.email

or

git config user.name

in your work directory to check your current config setting for the user's name or email.