Fundor 333

Advance Git Config and Ssh Config


Advance Git Config and Ssh Config

2021-03-11 | #dev #devops #dotfiles #git
Reading time 3 minutes | Word count 455 |

Many and many times I update my dotfiles1 and rewrite part of them for keep my work config and personal config in a same repo with all the config split for usage.

So I discover that I can have multiple ssh config and gitconfig on the same machine.

Multiple SSH config files

The SSH’s config file was too long to manage so one day I need to break it for make it easy for managing. So I implement one of the feature of the ssh config2: the include command.

How is it work?

You write your normal config ssh for work or personal user. Something like this for the personal config

Host fundor333
 User root
 HostName fundor333.com
 IdentityFile ~/.ssh/keys/fundor333
 PreferredAuthentications publickey

After this you do all the same for yor work setting and rename all like personal_config or job_config and write an empty setting as ~/.ssh/config.

So you have something like this

  • ~/.ssh/config
  • ~/.ssh/config-dir/personal_config
  • ~/.ssh/config-dir/job_config

Where config is empty. So I linking my stuff_config in to the main config with the import like this:

Include ~/.ssh/config-dir/personal_config
Include ~/.ssh/config-dir/job_config

In this way I can have sorted in a better way all my ssh config.

Multiple config for git

In reality I don’t need two gitconfig. I need two identity with git, one with some project one with others projects. Like ssh i need to use 2 account, one for work one for personal user.

In my case my repo structure is looking like this:

  • ~/Coding
  • ~/Coding/Personal
  • ~/Coding/Job

So I need to have three git identities:

  • A generic one for the default
  • A personal git identity for my personal project
  • A “job” git identity for my job project

How to do it

You need to write the .gitconfig and one file for every identities after the default one (so in this example three file, the gitconfig, the job_config and the personal_config).

So if you follow my example you have this

  • ~/.gitconfig
  • ~/.git-identities/job
  • ~/.git-identities/personal

where you need a default user for the .gitconfig linke this

[user]
email = [email protected]
name = Fundor333

and add the regex for the path of the directory of the projects with the path for the config like this:

[includeIf "gitdir:~/Github/**"]
path = ~/.git-identities/gitconfig-fundor333

[includeIf "gitdir:~/Coding/**"]
path = ~/.git-identities/gitconfig-fundor333

[includeIf "gitdir:~/Coding/Personale/**"]
path = ~/.git-identities/gitconfig-fundor333

[includeIf "gitdir:~/Coding/job/**"]
path = ~/.git-identities/gitconfig-job

If you put in the config define in the path the gitconfig you need you will override for the target project/s like for the gitconfig-job

[user]
email = [email protected]
name = Fundor333's Job account

This is a example for the user config but you can override all the config for the project like alias or the rule core for the commit and style of them.


  1. Dotfiles, bot and yaml files  ↩︎

  2. This examples need OpenSSH 7.3 or newer for work ↩︎

Mentions and response

Respond to this post on your own site. If you do, send me a webmention here.

Find out more about webmentions on the IndieWeb.