Working with Multiple Gitconfig

tech · Oct 8, 2022 · ~2 min
Photo by @praveentcom on Unsplash
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/.

1
2
3
$ mkdir -p "$HOME/.gitconfig.d/" && cd "$_"
$ pwd
/Users/clavianus.juneardo/.gitconfig.d

Setup Each Gitconfig

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

Setup Personal Gitconfig

1
2
3
4
$ git config --file=personal user.name "foo"
$ git config --file=personal user.email "[email protected]"
$ git config --file=personal core.editor "vim"
...

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

1
2
3
4
5
6
$ cat personal
[user]
	name = foo
	email = [email protected]
[core]
	editor = vim

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

Setup Work Gitconfig

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$ git config --file=work user.name "foo bar"
$ git config --file=work user.email "[email protected]"
$ 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 = [email protected]
	signingKey = ABCDEF012345
[commit]
	gpgsign = true
[core]
	editor = vim

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.

1
2
3
4
5
6
cat <<EOF > ~/.gitconfig
[includeIf "gitdir:~/Works/"]
  path = ~/.gitconfig.d/work
[includeIf "gitdir:~/Personals/"]"
  path = ~/.gitconfig.d/personal
EOF

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!

· · ·

Love This Content?

Any kind of supports is greatly appreciated! Kindly support me via Bitcoin, Ko-fi, Trakteer, or just continue to read another content. You can write a response via Webmention and let me know the URL via Telegraph.

Drop Your Comment Below