How I Am Maintaining Multiple Emails For Git On A Same Machine
Every git commit is associated with two important data: your name and email. I don’t want my personal email address associated with work related git commits and vice versa. First, to set the git email address system wide, you would run following:
$git config --global user.email "[email protected]"
$git config --global user.name "Your Name"
and every commits will have above info. To set the email address for individual repo, just drop the global
. cd
into your repository and run the following:
$git config user.email "[email protected]"
$git config user.name "Your Name"
Now every commit for this repository will have above email. There is another way, by modifying .git/config
of your repository and including a [user]
block, something like:
[core]
...
[remote "origin"]
...
[branch "master"]
...
[user]
name = Your Name
email = [email protected]
Problems
Though above mentioned methods work, there are two major issues (at least for me):
- You have to run the above command everytime you create a new repository.
- You have to remember #1.
#2 is actually difficult for me.
Solution
Use direnv
. direnv
is one nifty tool which lets you have different environment variables based on directories/path. The best part is, as soon as you enter into a directory, direnv
does it’s magic, so you don’t have to remember that you have to run direnv
. For direnv
to work, you have to create a file called .envrc
where you can specify what all environment variables you want and place it the directory.
This is how I have organized my repositories:
~/
|- work # all work related repos go here
|-- .envrc
|-- repo-1
|-- ...
|-- repo_X
|- Documents/code # all my personal projects go here
|-- .envrc
|-- repo-1
|-- ...
|-- repo_X
So, my ~/work/.envrc
contains:
export GIT_AUTHOR_EMAIL="[email protected]"
export GIT_COMMITTER_EMAIL="[email protected]"
similarly, my ~/Documents/code/.envrc
contains:
export GIT_AUTHOR_EMAIL="[email protected]"
export GIT_COMMITTER_EMAIL="[email protected]"
Before each prompt direnv
checks for .envrc
in current directory and parent directories. And when the file is found, it applies those and those variables will be present in your shell. You can also add GIT_AUTHOR_NAME
or GIT_COMMITTER_NAME
if you want to use different names in git commits.
References:
- Pro Git book on environment variables.
direnv