DEV Community

Sebastian Bille
Sebastian Bille

Posted on • Edited on

27 6

Conditional git profile configuration

I always found it inconvenient that I had to either keep changing the email in my .gitconfig every time I wanted to switch between working on a personal project and a work project, or I had to remember to configure the email in each repository as I cloned it. I often forgot and git discipline being something of a pet peeve of mine, I spent way too much time rewriting history just to change my darned email address so that it wouldn't be inconsistent in the git log. I have a problem, I know but let's keep on topic here mmhkay?

Turns out, there's a much easier way to do this. It's possible to conditionally extend the git configuration based on where your repository is located in your file system. That means we can have a global git configuration with all the default configuration and then in a few overrides for all repositories within, for example, the ~/code/work/ folder by adding a little something like this to the ~/.gitconfig file.

// ~/.gitconfig
[user]
    name = Sebastian Bille
    email = personal@example.com

[includeIf "gitdir:~/code/work/"]
    path = ~/code/work/.gitconfig
Enter fullscreen mode Exit fullscreen mode
// ~/code/work/.gitconfig
[user]
    email = work@example.com
Enter fullscreen mode Exit fullscreen mode

Now all repositories inside of the ~/code/work/ folder will use your overridden email but all others will keep using the default configuration.

This could of course be used to override or add any other configuration conditionally as well.

If you enjoyed this guide and want to see more, follow me on Twitter at @TastefulElk where I frequently write about serverless tech, AWS and developer productivity!

Happy hacking! 🚀

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
pratapkute profile image
Pratap kute

That just great....😃

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Awesome! Just what I needed.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay