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
repositories.
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 email.foo@example.com
$ git config user.name name-foo
The above git config
commands write the user.email
setting into the .git/config
file under the repository.
From 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
user.email=email.bar@example.com
user.email=email.foo@example.com
The 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.
From git help config
,
When reading, the values are read from the system, global and repository local configuration files by default
The git config --list
is a read operation.
The first user.email
value above is from “global”, i.e. ~/.gitconfig
.
Run git config --list --local | grep user.email
to check the repository-local email configuration.
Instead of piping config --list
and grep
, use git config --get user.email
to save some typings.
$ cd /path/to/repo-foo
$ git config --get user.email
user.email=email.foo@example.com
From git help config
, --get
returns
the last value if multiple key values were found.
Here, the last value is the email from repository-local configuration.
The --get
can be further omitted, git config user.email
has the same result.
And git config --get-all user.email
is same as git config --list | grep user.email
.
Conditional Includes
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 ~/corp-repo/
.
Add a text file called corp-gitconfig
there, and edit it as below.
[user]
name = user-name-for-corp-prj
email = email-add@your-corp.com
Add below lines in the global git config file, i.e. ~/.gitconfig
.
[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
email-add@your-corp.com
.