DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

4

Working with Multiple Gitconfig

Photo by @praveentcom on Unsplash

Introduction

This article will be a quick tips if you're using multiple git config inside one local machine. For example if you're working on both Gitlab/Bitbucket/Github with different email/username/gpgsign, or you're working on your personal and work git account on the same machine.

Setup Gitconfig Directory

This directory name would be anything, but for the sake of naming convention let's call it ~/.gitconfig.d/.

$ mkdir -p "$HOME/.gitconfig.d/" && cd "$_"
$ pwd
/Users/clavianus.juneardo/.gitconfig.d
Enter fullscreen mode Exit fullscreen mode

Setup Each Gitconfig

Now you have create the directory, let's say you want to set your personal and work account:

Setup Personal Gitconfig

$ git config --file=personal user.name "foo"
$ git config --file=personal user.email "foo@gmail.com"
$ git config --file=personal core.editor "vim"
...
Enter fullscreen mode Exit fullscreen mode

By using --file=personal flag, the gitconfig will be configured inside personal file.

$ cat personal
[user]
    name = foo
    email = foo@gmail.com
[core]
    editor = vim
Enter fullscreen mode Exit fullscreen mode

Now let's do the same with your work account.

Setup Work Gitconfig

$ git config --file=work user.name "foo bar"
$ git config --file=work user.email "foo@company.com"
$ git config --file=work user.signingKey "ABCDEF012345"
$ git config --file=work commit.gpgsign true
$ git config --file=work core.editor "vim"
...

$ cat work
[user]
    name = foo bar
    email = foo@company.com
    signingKey = ABCDEF012345
[commit]
    gpgsign = true
[core]
    editor = vim
Enter fullscreen mode Exit fullscreen mode

Now you have set both your account. Then, how to switch between each gitconfig?
Let's say you put all your company's git directory at ~/Works and yours at ~/Personals. You can switch easily by configure the global gitconfig using includeIf.

cat <<EOF > ~/.gitconfig
[includeIf "gitdir:~/Works/"]
  path = ~/.gitconfig.d/work
[includeIf "gitdir:~/Personals/"]"
  path = ~/.gitconfig.d/personal
EOF
Enter fullscreen mode Exit fullscreen mode

Conclusion

Now everytime you're inside ~/Works/ you are using the ~/.gitconfig.d/work, and when you're inside ~/Personals/ you are using the ~/.gitconfig.d/personal.

You can check whether the gitconfig load properly or not by simply executing git config user.email command and see what email is showing up.

Thank you for reading!

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (2)

Collapse
 
sherrydays profile image
Sherry Day

Great post

Collapse
 
clavinjune profile image
Clavin June

@sherrydays thanks!

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay