Managing multiple Git configurations

Rafael Almeida
Nov 27, 2020 · 3 min read
Photo by Yancy Min on Unsplash

Using the same computer for both work-related and personal projects may cause you to write Git commit messages with your private email on your work projects or vice-versa. That is assuming that you configured Git with the --global flag, which applies the configs to every repo on your OS user account.

But, what if there was an easy way to separate your Git configs, whether you’re dealing with private or work repositories, accordingly? I have great news for you: if you’re using Git with, at least, version 2.13, you can have Conditional Includes.

You might be wondering:

“That’s great. But how does it help me?”

Worry no more!

The “middle” Git config level is the global one, which means that your configurations are stored on a ~/.gitconfig file. Wondering how that file looks? Here’s mine:

~
▶ cat .gitconfig
[user]
signingkey = C9KF0E358V41E9C5
name = Rafael Almeida
email = rafaelcpalmeida@----.com
[gpg]
program = gpg
[commit]
gpgsign = true

Now, let’s assume that I wanted to use:

The email rafaelcpalmeida@— .com with the GPG key C9KF0E358V41E9C5 on my private repositories.

and

The email rafael.almeida@company.com with the GPG key 9VKF4E35KV4LE935 on my work repositories.

That could be easily achieved by creating two new files: one with the configurations for your personal repositories and one for the work-related ones. Here are mines:

~
▶ cat .gitconfig_personal
[user]
signingkey = C9KF0E358V41E9C5
name = Rafael Almeida
email = rafaelcpalmeida@----.com

Now, all I have to do is to reference them on my original .gitconfig file. Again, here’s mine, now updated:

~
▶ cat .gitconfig
[gpg]
program = gpg
[commit]
gpgsign = true
[includeIf “gitdir:~/ydata/”]
path = .gitconfig_ydata
[includeIf “gitdir:~/projects/”]
path = .gitconfig_personal

The file above uses the configuration of each of the files but only if the path where the repository I’m interacting with matches the path specified on the configuration file.

Here’s a demo:

~/ydata
▶ mkdir test && cd test

Looking at my git log, the output I get is:

commit 6d5b06ef2678b921436b1c17a1d5684302c9a722 (HEAD -> master)
Author: Rafael Almeida <rafael.almeida@company.com>
Date: Tue Nov 24 00:30:21 2020 +0000

Wondering what happens if I do the same on ~/projects? Here:

~/projects
▶ mkdir test && cd test

And here’s my git log:

commit 2ee78dcd2d154ff227982ad2e04644dd98f64c6c (HEAD -> master)
Author: Rafael Almeida <rafaelcpalmeida@----.com>
Date: Tue Nov 24 00:33:30 2020 +0000

And this is how you can manage different Git configurations without any fuzz. Want to share your feedback? Let me hear you in the comments below!

YData

Improved data for AI

YData

YData is a data-centric platform that democratizes the access to valuable data and allows Data Scientists to build and deploy better AI solutions with high-quality and synthetic data

Rafael Almeida

Written by

Hey! I write about several stuff, mostly technical or software development related, often switching from Portuguese to English. Doing stuff at @YData_ai

YData

YData is a data-centric platform that democratizes the access to valuable data and allows Data Scientists to build and deploy better AI solutions with high-quality and synthetic data

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store