How to have multiple Git configs for the same user
amabe_dev
Posted on July 14, 2022
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/
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>
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
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
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
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 :)
Posted on July 14, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.