DEV Community

Cover image for Working with multiple git configurations
Hasan TEZCAN
Hasan TEZCAN

Posted on • Updated on

Working with multiple git configurations

I have started to work as a full-time software engineer at a company. Therefore I had to clone lot of projects and start to commit these repositories with my company email.

I had already defined my personal email as a global git config like this

git config --global user.email "hasantezcan@personal.com"
Enter fullscreen mode Exit fullscreen mode

As you can see, my personal email defined as a global config so I have to define my company email as a local config after every single new cloning.

git config --local user.email "hasantezcan@company.com"
Enter fullscreen mode Exit fullscreen mode

But sometimes I forget to define my company mail and have to reset my commits and resend it again. It's definitely time consuming to do this!

I need a persistent solution for this issue.

And I have encountered with .gitconfig file!

.gitconfig

Actually when you set the global email and username you change the .gitconfig file which belongs in user root directory. (the directory that you type cd and enter in terminal prompt)

So what I need is, I have to declare a spacial email under workspace/company directory. Because I download all repository and handle my all business in there.

To solve this issue we can use includeIf setting in global .gitconfig file.

includeIf provides us to change the config file for a specific directory.

So let's get started

First, go to the directory where your company projects are located. For example let's say it is Workspace/CompanyName.

cd Workspace/CompanyName
Enter fullscreen mode Exit fullscreen mode

Create .gitconfig-CompanyName file

touch .gitconfig-CompanyName
Enter fullscreen mode Exit fullscreen mode

Define your company specific configs in there. After this, you will start the use those configs under this directory.

// /Users/hasantezcan/CompanyName/.gitconfig-companyName
[user]
    name = hasantezcan
    email = hasantezcan@company.com
Enter fullscreen mode Exit fullscreen mode

Now we have to update the global .gitconfig for a special .gitconfig-CompanyName file that will use under this directory.

So let's get declare this;

Go to your user home directory

cd 
pwd User/hasantezcan
Enter fullscreen mode Exit fullscreen mode

and update .gitconfig file like this.

[user]
    name = hasantezcan
    email = hasantezcan77@gmail.com
+[includeIf "gitdir:~/Workspace/CompanyName/"]
+   path = ~/Workspace/CompanyName/.gitconfig-companyName
Enter fullscreen mode Exit fullscreen mode

Voilà! 🎉 Now you can continue to commit your opensource project without any wrong commit email problem.

Reference

Top comments (0)