Originally published in WyeWorks blog.
There was a time when I found myself constantly switching between my personal and work computers, and it was really annoying. After some time switching back and forth, I decided to use just one computer.
After settling on the single computer approach, what irritated me most was having to remember to switch my Git username, email, and SSH keys when moving from a work repository to a personal repository and vice versa. I would frequently forget to do that and, every now and then, I would find work commits tagged with my personal email or the other way around.
At work, most of the repositories are private, but at home most of my work is public, which means that my work email became public, and I didn't like that. Hence, in this post I will share one way you can go about forgetting to switch back and forth and let the computer do it automatically for you.
If you are utilizing SSH to interact with your Git server (probably GitHub), continue on, dear reader. Otherwise, skip this step. (Or, even better: Start using SSH!)
Let's say you have two SSH key-pairs, one for
email@example.com and one for
firstname.lastname@example.org. You’ll want to use your corresponding SSH key-pair when pushing, cloning, or otherwise interacting with a work or personal repository, respectively. And that's easy enough to accomplish - just create or modify an existing
~/.ssh/config file that looks similar to this:
#personal Host github.com-personal HostName github.com User personal IdentityFile ~/.ssh/id_rsa_personal PreferredAuthentications publickey #work Host github.com-work HostName github.com User work IdentityFile ~/.ssh/id_rsa_work PreferredAuthentications publickey
personal with your personal Git username and
work with your work-related one, and we’re done with this step.
When cloning your repo you should use you custom Host configuration, if you normally clone
git clone email@example.com:personal/repo.git now you should use
git clone firstname.lastname@example.org:personal/repo.git. If you want to use this in existing repositories just update the remotes.
For this step, I am assuming you have all of your work stuff contained in a
~/work folder and all of your personal stuff contained in a
~/personal folder. If not, then you will either have to move some things around on your computer or adapt this step to your individual needs.
In each folder, add a file named
.gitconfig. You’ll end up with one
~/work/.gitconfig file and one
~/personal/.gitconfig file. In each file, you should fill in your information accordingly. For example:
[user] email = email@example.com name = John Doe
[user] email = firstname.lastname@example.org name = John Doe
Step two, done! Now let's move on to the final step where we slap everything together.
In this, our final step, we are going to tell Git to use different configurations depending on what you’re working on. First, you need to create or modify the
~/.gitconfig file (if one already exists) and change it to look something like this:
[includeIf "gitdir:~/work/"] path = ~/work/.gitconfig [includeIf "gitdir:~/personal/"] path = ~/personal/.gitconfig
This tells Git to load the
~/work/.gitconfig file when you are in any folder under
~/work, and the other configuration when you are under
Now you have configured Git such that it will automatically select which user, email, and key-pair to utilize based on what you are working on. You can forget about accidentally mixing up usernames and you’ll no longer have to worry about disclosing your personal email at work, or, even worse, disclosing your work email to the public.
You can further customize each file’s configuration and end up customizing Git for each environment. For example, you can configure your repositories to execute some hooks, as I explained in my previous post, Using git hooks to improve your day-to-day workflow.
Hopefully this info proved useful. One way or another, let me know by leaving a comment!