Back

Directory based config for easy git workflow

Background

A very common use case for a developer is to deal with multiple github accounts and hence multiple ssh keys. Usually you do that by specifying the ssh-key when you want to deal with another account with the ssh config.

git clone -c "core.sshCommand=ssh -i ~/.ssh/<privateKeyName>" git@github.com:<user>/<repo>.git

and this gets painful pretty quickly. This is not only the case for some private and company repositories, but also if you are working for an agency and you have to deal with multiple clients. Also its a bit okay if you have the projects on different hosts (ie: github and gitlab), meaning: you can reuse your ssh key for those hosts and the git clone/push/pull works as expected. But if you have them in the same host with different accounts then its problematic because then they don't allow you to add/upload same public key for multiple accounts.

But here is the good news. You can manage this pretty easy if you are keeping projects in different directories. Let's say your Personal projects are in ~/Personal directory and company code is in ~/Work directory. Or ~/Projects/Personal and ~/Projects/Work etc... (let's take the latter example for the sake of some silly complexity in our post)

The trick is to tell git that, when you are in a specific directory then use a specific gitconfig (which is/will be stored in .gitconfig file in the directory)

Let's quickly jump into the actual configuration

First we have to go to the parent directory of our repositories ie: ~/Projects in our case, and create a .gitconfig file , and add the following content inside it

~/Projects/.gitconfig
[includeIf "gitdir:Projects/Work"]
  path = ~/Projects/Work/.gitconfig
[includeIf "gitdir:Projects/Personal"]
  path = ~/Projects/Personal/.gitconfig"

basically we are telling git that: if you are in the specified directory, then load the gitconfig from that directory instead of the global gitconfig.

Then we have to create those .gitconfig files in the respective directories

ie:

touch ~/Projects/Personal/.gitconfig
touch ~/Projects/Work/.gitconfig

Now before we get to edit the contents of those .gitconfig files, make sure to create 2 separate ssh keys. Have a look here for information on generating ssh keys.

ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_personal_key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_work_key

now open the ~/Projects/Personal/.gitconfig file and add the following inside as its content

# replace contents as neccessary
[user]
email = username@example.com
name = Jhon Doe

[github]
user = "example_user"

[core]
sshCommand = "ssh -i ~/.ssh/my_personal_key"

basically we are telling git to use a specific config when you are in that directory (Here its the Personal directory)

likewise also add the following as the content inside the ~/Projects/Work/.gitconfig file

# replace contents as neccessary
[user]
email = my_work_email@work.com
name = My Name

[github]
user = "work_username"

[core]
sshCommand = "ssh -i ~/.ssh/my_work_key"

Now git wil use this information for any git related commands when you are executing something inside that directory or its child directories.