loading...
Cover image for Automatically managing personal and work git configurations

Automatically managing personal and work git configurations

fedekau profile image Federico Kauffman ・3 min read

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.

Step 1: Configure your SSH keys

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 user@work.com and one for user@personal.com. 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

Replace personal with your personal Git username and work with your work-related one, and we’re done with this step.

Edit

When cloning your repo you should use you custom Host configuration, if you normally clone git clone git@github.com:personal/repo.git now you should use git clone git@github.com-personal:personal/repo.git. If you want to use this in existing repositories just update the remotes.

Step 2: Add user details to work and personal folders

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 = user@work.com
  name = John Doe
[user]
  email = user@personal.com
  name = John Doe

Step two, done! Now let's move on to the final step where we slap everything together.

Final step: Overriding git configuration based on current folder

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 ~/personal.

Conclusion

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!

Discussion

pic
Editor guide
Collapse
ericcurtin profile image
Eric Curtin

Note you must be running git version >= 2.13 for this feature, you can check this via:

git --version

If your package manager does not provide at least this version, remove that version, compile and install your own:

cd /tmp &&\
wget kernel.org/pub/software/scm/git/gi... &&\
tar xf git-2.13.1.tar.gz &&\
cd /tmp/git-2.13.1 &&\
make configure &&\
./configure --prefix=/usr &&\
make -j3 all doc &&\
make -j3 install install-doc &&\
rm -rf /tmp/git-2.13.1*

Any problems, reply and I'll try my best to help you out!

Collapse
ferricoxide profile image
Thomas H Jones II

Wait: you're not GPG-signing your commits (and using work/personal signing-keys as appropriate)? Heresy! ;)

Collapse
fedekau profile image
Federico Kauffman Author

In that example I don't sign commits, it was not the main purpose of the post, but you could of course do that. I wouldn't say it is heresy, I just didn't mentioned it :)

Collapse
themattyg profile image
Matt Graham

This is great with one caveat on my end: in their infinite wisdom, the IT department at my employer blocks SSH. 🙄

Collapse
dogers profile image
Dogers

You should be able to do similar with tokens and HTTPS connections instead

Collapse
dogers profile image
Dogers

Rather than changing the URL you use, why not add it to your two .gitconfig files to rewrite it automatically?

[url "git@github.com-work"]
insteadOf = git://github.com

etc

Collapse
fedekau profile image
Federico Kauffman Author

That is very good idea actually, I will try it, thanks!

Collapse
msoedov profile image
Alex Miasoiedov

I have done the same with a simple bash function, it's probably less convenient auto switching in ~/.gitconfig :)

Collapse
fedekau profile image
Federico Kauffman Author

There are probably a lot of ways to do this, but in my opinion this is pretty handy and fast, that is why I shared it :)