Managing multiple Git profiles

May 4, 2020

Managing multiple Git profiles

If you use git as the version control tool for both your personal projects and at work, chances are that you want to isolate the two (or more) profiles, i.e. at the very least, using different emails for creating commits and tags. Other uses might include using different usernames (for different GitHub accounts), gpg-keys, etc.

One straightforward way of tackling this is to remember setting the correct configuration whenever you clone (or initialize) a new repo:

git config user.name "priam"
git config user.email "[email protected]"

Besides, being painstakingly mundane, it is yet another thing to remember. Nevermind the time and effort of rewriting history if you have already created commits.

But wait, there's a better way

Git offers a much more flexible way of specifying conditional config files based on your current directory — if you use a different directory for all your work repos (let's say ~/work/), then you can specify the following in your ~/.gitconfig to automatically use your work credentials inside them:

[include]
    path = ~/git-personal.conf
[includeIf "gitdir:~/work/"]
    path = ~/git-work.conf

Each conf file can then define it's own user, e.g. ~/git-personal.conf:

[user]
    name = realerlich
    email = [email protected]
    signingkey = FF5353EC154B6811

and ~/git-work.conf:

[user]
    name = erlich
    email = [email protected]
    signingkey = CE3454AA132E6F2E

There, all set now! Whenever you are working inside a repo that lives under ~/work, Git will automatically use your work email. Go ahead, try it out!

Get started with DeepSource

DeepSource is free forever for small teams and open-source projects. Start analyzing your code in less than 2 minutes.

Choose an account
Newsletter

Read product updates, company announcements, how we build DeepSource, what we think about good code, and more.