DEV Community

Cover image for How To Set Multiple Git Identities With Git Config
Muhammad Ilham hidayat
Muhammad Ilham hidayat

Posted on

How To Set Multiple Git Identities With Git Config

This article is originally posted from Hashnode

Prerequisite

  • Git 2.13
  • Linux Command
  • 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 2020 +0700

feat: implement zookeeper to handle concurrent lock
Enter fullscreen mode Exit fullscreen mode

--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?.

UserName Email Git Repository
john john@doe.com GitHub
John Doe john.doe@mail.com GitLab

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 2020 +0700

feat: implement zookeeper to handle concurrent lock
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Open ~/.gitconfig in your home directory. You can set two conditional includes config for your personal project and work repositories. In personal directory, Git will use .gitconfig-personal config file. For work directory, Git will use gitconfig-work.
[includeIf "gitdir:~/personal/"] 
  path = .gitconfig-personal 

[includeIf "gitdir:~/work/"] 
  path = .gitconfig-work 
Enter fullscreen mode Exit fullscreen mode
  • Still in home directory, create Git config for personal project and work repositories. You can set Git identity for those Git config.
$ touch .gitconfig-personal && touch .gitconfig-work

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

// .gitconfig-work
[user]
name = John Doe
email = john.doe@mail.com
Enter fullscreen mode Exit fullscreen mode
  • Now you will have three Git config files in home directory
.gitconfig
.gitconfig-personal
.gitconfig-work
Enter fullscreen mode Exit fullscreen mode
  • Let's verify the git config. Create and initiate a new git repo in your personal project and work repositories. There will be a difference 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
...
Enter fullscreen mode Exit fullscreen mode
// 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
...
Enter fullscreen mode Exit fullscreen mode

Source

Top comments (1)

Collapse
 
rajan_001 profile image
Rajan

Great article. Thanks for sharing 😊