DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

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!

Top comments (2)

Collapse
 
sherrydays profile image
Sherry Day

Great post

Collapse
 
clavinjune profile image
Clavin June

@sherrydays thanks!