How To Set Multiple Git Identities With Git Config

How To Set Multiple Git Identities With Git Config

When you have personal project repositories and work repositories

Prerequisite

  • Git 2.13

  • macOS / Linux


The first thing you should do after installing Git is to set your Git config identity (user name and email address). This step is important because every Git commit uses this information.

$ git config --global user.name "john"
$ git config --global user.email "john@doe.com"
$ git log

commit 35ba69d62d8495cdfee225b5b721b5fc07115ba1 
Author: john <john@doe.com>
Date:   Tue January 12 10:57:25 2021 +0700

feat: implement zookeeper to handle concurrent lock

--global is an option to set global Git config identity (username and password) for the current operating system user. Every repository will use that Git identity.

But if you're using Git for your personal project and work, usually you have two Git identities. How to make sure you use the right Git identity for your personal project and work?

User NameEmailGit RepositoryDescription
johnGitHubFor personal
John DoeGitLabFor work

One way to make sure you commit using your work Git Identity in your work repositories is to set Git config identity on repository level.

On the repository level, Git config will apply to a single repository. It will not work outside that repository.

$ git config user.name "John Doe"
$ git config user.email "john.doe@mail.com"
$ git log

commit 35ba69d62d8495cdfee225b5b721b5fc07115ba1 
Author: John Doe <john.doe@mail.com>
Date:   Tue January 12 14:57:25 2021 +0700

feat: implement zookeeper to handle concurrent lock

The main downside is for this approach is you need to set work Git identity for every work repository. Is it possible to apply your work Git config identity to your all work repositories?

Starting from Git 2.13 Git implemented a feature called condition configuration includes.

You can set multiple Git configs for a specific directory in your home directory ~/.gitconfig file using includeIf keyword. Using this feature, you can set Git config for your personal project and work repositories.

Set Multiple Identities With Git Config

  • In your home directory, create separate directories for personal project and work repositories
$ mkdir personal && mkdir work

├── personal // for personal project repositories
├── work // for work repositories
  • Open ~/.gitconfig in your home directory. You can set two conditional includes config for your personal project and work repositories.
# For personal project, use .gitconfig-personal
[includeIf "gitdir:~/personal/"] 
  path = .gitconfig-personal 

# For work, use .gitconfig-work
[includeIf "gitdir:~/work/"] 
  path = .gitconfig-work
  • In home directory, create Git config for personal project and work repositories. You can set Git identity for those Git configs.
$ touch .gitconfig-personal && touch .gitconfig-work

// set user name and email for .gitconfig-personal
[user]
name = john
email = john@doe.com

// set user name and email .gitconfig-work
[user]
name = John Doe
email = john.doe@mail.com
  • Now you will have three Git config files in home directory
.gitconfig
.gitconfig-personal
.gitconfig-work
  • Let's verify the git config. Create and initiate a new git repo in your personal project and work repositories. There will be differences in user name and email for repositories inside personal and work directory.
// personal directory
$ cd ~/personal
$ mkdir personal-repo
$ cd personal-repo
$ git init
$ git config -l

...
user.name=john
user.email=john@doe.com
...
// work directory
$ cd ~/work
$ mkdir work-repo
$ cd work-repo
$ git init
$ git config -l

...
user.name=John Doe
user.email=john.doe@mail.com
...

Source