Sometimes, we may want to set up different user emails and user names for different Git repositories.
For example, in your personal computer, the
user.email is set to your personal email address globally.
While committing to your corporate repositories in the personal computer, your corporate email address should be used in the
commits. Or you’re working on personal projects on the corporate computer, need to use the personal email for the personal
Configure Email Address for A Repository
The simplest way is going to each repository, and configuring the user email for each repository specifically.
$ cd /path/to/repo-foo $ git config user.email firstname.lastname@example.org $ git config user.name name-foo
git config commands write the
user.email setting into the
.git/config file under the repository.
git help config, when writing configuration, by default it’s writing to the repository-local configuration file.
When writing, the new value is written to the repository local configuration file by default, and options –system, –global, –worktree, –file
can be used to tell the command to write to that location
Examine the Email Address for a Repository
$ cd /path/to/repo-foo $ git config --list | grep user.email email@example.com firstname.lastname@example.org
git config --list above prints more than one
user.email values. It’s because, without additional options, the
git config --list outputs configuration “merged” from system, global and local.
git help config,
When reading, the values are read from the system, global and repository local configuration files by default
git config --list is a read operation.
user.email value above is from “global”, i.e.
git config --list --local | grep user.email to check the repository-local email configuration.
Instead of piping
config --list and
git config --get user.email to save some typings.
$ cd /path/to/repo-foo $ git config --get user.email email@example.com
git help config,
the last value if multiple key values were found.
Here, the last value is the email from repository-local configuration.
--get can be further omitted,
git config user.email has the same result.
git config --get-all user.email is same as
git config --list | grep user.email.
For new cloned repositories, it’s often to forget to configure the right email addresses for them.
The “conditional includes” feature of the
git config can save us from this problem.
For example, in your personal computer, all corporate repositories are under
Add a text file called
corp-gitconfig there, and edit it as below.
[user] name = user-name-for-corp-prj email = firstname.lastname@example.org
Add below lines in the global git config file, i.e.
[includeIf "gitdir:~/corp-repo/"] path = ~/corp-repo/corp-gitconfig
Now if a new repository is cloned user
~/corp-repo/, the email for that repository is automatically set to