How to Use Multiple Git Configs on One Computer


If you work on different projects with your computer, it may be necessary to use different credentials. There is a simple solution for this by customizing the .gitconfig and creating different git profiles.

To make this work, we organize our projects into different folders. Since I use ghq to clone my projects, we will use this structure to customize our Git config.

Screenshot of a console output that shows multiple folders

Let’s assume that we have 3 different folders. In this case the following:

  • git.hashdot.co
  • github.com
  • ssh.dev.azure.com

All subfolders then use our created configs. Let’s start creating them.

First we create a global Git config in our home directory:

touch ~/.gitconfig

Then we add the following content:

[includeIf "gitdir:~/git.hashdot.co/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/ssh.dev.azure.com/"]
  path = ~/.gitconfig-work

Now we need to create the two configs as follows:

touch ~/.gitconfig-personal

touch ~/.gitconfig-work

In the personal config we add the following content:

[user]
  name = Your Name
  email = Your personal email

In the work config we add the following content:

[user]
  name = Your Name
  email = Your work email

To test that it works, we can go to any subdirectory and execute the following command:

git config --list

Now something like the following should appear:

core.editor=nvim
includeif.gitdir:~/ghq/git.hashdot.co/.path=~/.gitconfig-personal
user.name=Lars Hampe
user.email=hello@hashdot.co
includeif.gitdir:~/ghq/ssh.dev.azure.com/.path=~/.gitconfig-work
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
submodule.active=.
remote.origin.url=https://git.hashdot.co/HashDot/website.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main