DEV Community

amabe_dev
amabe_dev

Posted on • Updated on

How to have multiple Git configs for the same user

How to have multiple Git configs for the same user?

In this article, we will see how to have different Git configs depending on the folder in which is the project we are working on.

It is quite easy to change the config for a single project. But it means we have to do it for every project on which we want the change to be made. And we need to keep it in mind to do it on every new project that we clone.

The goal here is to set it up once and be able to forget it. We will then be able to clone any new project and it will use our custom config without us doing anything.


Why have multiple Git configs?

There are multiple reasons why we may want to have multiple Git configs. For example:

  • To have a different commit name and email on different projects. For example, for work projects and for personal projects.
  • To have different aliases depending on projects. For example, if some aliases are tied to a context shared by only some projects.
  • To have different behavior on different projects. For example, having git pull perform a merge on some project and a rebase on others.

I can go on and on. But I might end up reciting everything that can be configured in Git. So, I won't.


What we will do

To have different configs depending on projects, we will use a Git option to include config files conditionally.

For the example, we are going to use a different name depending if we are on a work project or a personal project.

We will assume that we have a workspace structure like this:

~/
    work/
        project1/
        project2/
    perso/
        project3/
        project4/
Enter fullscreen mode Exit fullscreen mode

Diving into Git configs

First, we need to create config files with the config that we want to override. For example ~/work/gitconfig and ~/perso/gitconfig and add the custom config in each.

For example, in ~/work/gitconfig:

[user]
    name = The name I use at work
    email = <myworkemail@example.com>
Enter fullscreen mode Exit fullscreen mode

Then we can conditionally include the config from the folder depending on the project on which we are working on. To do this, we will add a new option in out Git config (~/.gitconfig):

[includeIf "gitdir:~/work/**"]
    path = ~/work/gitconfig
Enter fullscreen mode Exit fullscreen mode

Now, each time we run a Git command in a directory inside ~/work/ folder, the custom config from ~/work/gitconfig will override our default config. And, if we run a command anywhere else, it will not.

Only the config that is present in the included file will be overridden. Anything else from the ~/.gitconfig will stay unchanged.

This is helpful to have a common config for everything. With only small differences for some projects.

(We can ever have aliases in the main config file and add new aliases in an included file.)


Using different SSH keys

When using different identities, we might want to also use different SSH keys. For example, to use different accounts on the same Git repo hosting platform (GitHub, GitLab, etc).

To do this, we will use both the SSH config file and an option from Git to change the URL used on actions.

To be able to use different SSH keys, we will update the SSH config (~/.ssh/config) like this:

Host github
    User git
    HostName github.com
    IdentityFile ~/.ssh/id_rsa # change the path to the key here
    IdentitiesOnly true
Enter fullscreen mode Exit fullscreen mode

This will use the SSH key at the given path when using github as a remote (here ~/.ssh/id_rsa).

Then we need to replace the used remote from git@github.com to github to use our new SSH alias. We can either update the remote on each repository or add this in our Git config (~/.gitconfig):

[url "github"]
    insteadOf = git@github.com
Enter fullscreen mode Exit fullscreen mode

Conclusion

By using this configs, we are able to have a flexible Git config. Some is common between projects because we want to use Git in the same way everywhere. And yet we are able to override some configs depending on the projects we are working on.

I found it really helpful (and quite hard to find) when I needed to use a different commit email on some projects.

If you have any feedback, please reach out either in the comments or in private. This is my first attempt at learning in public and writing about technical things. I will be grateful for any opportunity to improve.

Hope it helped! And have a good config session :)

Top comments (6)

Collapse
 
maymeow profile image
May Meow

I using multiple git configs similar to you with small differences. Instead of using sshconfig to tell which key to use on which domain i added to my .gitconfig file following command
sshCommand = β€œssh -i ~/.ssh/<professional_key>” in [core] directive where <professional_key> is public key file name

And with combination of 1password ssh agent is work flawlesly (no more copying private keys between computers)

Collapse
 
amabe_dev profile image
amabe_dev

Nice to know, I didn't know about this possibility. Thanks for sharing!

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Have you considered using a Git GUI client like GitKraken? All of this stuff is a breeze

Collapse
 
amabe_dev profile image
amabe_dev • Edited

I tried some and some IDE integration but I didn't stick with any. I only use JetBrain IDE for the mergetool (with the community version of IntelliJ IDEA) because it is the best I have found yet.

Otherwise, I got used to the CLI first. And everytime I tried a Git GUI I found it too abstract and was not sure of what I did. And I got some aliases that make the CLI way faster.

Collapse
 
dthompsonza profile image
Dave Thompson (ツ)

Didn't want to pay $5/month for GitKraken so I use Git Fork. Really the best free Git GUI out there imo

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

No Linux version :(