This article is originally posted from Hashnode
- 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 "email@example.com" $ git log commit 35ba69d62d8495cdfee225b5b721b5fc07115ba1 Author: john <firstname.lastname@example.org> Date: Tue January 12 10:57:25 2020 +0700 feat: implement zookeeper to handle concurrent lock
--globalis 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?.
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 "email@example.com" $ git log commit 35ba69d62d8495cdfee225b5b721b5fc07115ba1 Author: John Doe <firstname.lastname@example.org> Date: Tue January 12 14:57:25 2020 +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.
- 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
~/.gitconfigin your home directory. You can set two conditional includes config for your personal project and work repositories. In
personaldirectory, Git will use
.gitconfig-personalconfig file. For
workdirectory, Git will use
[includeIf "gitdir:~/personal/"] path = .gitconfig-personal [includeIf "gitdir:~/work/"] path = .gitconfig-work
- 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 = email@example.com // .gitconfig-work [user] name = John Doe email = firstname.lastname@example.org
- 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 a difference in user name and email for repositories inside
// personal directory $ cd ~/personal $ mkdir personal-repo $ cd personal-repo $ git init $ git config -l ... user.name=john email@example.com ...
// work directory $ cd ~/work $ mkdir work-repo $ cd work-repo $ git init $ git config -l ... user.name=John Doe firstname.lastname@example.org ...