Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git config with multiple identities and multiple repositories

Setup multiple git identities & git user informations

/!\ Be very carrefull in your setup : any misconfiguration make all the git config to fail silently ! Go trought this guide step by step and it should be fine 😉

Setup multiple git ssh identities for git

  • Generate your SSH keys as per your git provider documentation.

  • Add each public SSH keys to your git providers acounts.

  • In your ~/.ssh/config, set each ssh key for each repository as in this exemple:

      Host github.com
      	HostName github.com
      	User git
      	IdentityFile ~/.ssh/github_private_key
      	IdentitiesOnly=yes
      Host gitlab.com
      	Hostname gitlab.com
      	User git
      	IdentityFile ~/.ssh/gitlab_private_key
      	IdentitiesOnly=yes

Setup dynamic git user email & name depending on folder

/!\ Require git 2.13+ for conditional include support.

The idea here is to use a different git user name & email depending on the folder you are in.

  • In your ~/.gitconfig, remove the [user] block and add the following (adapt this exemple to your needs) :

      [includeIf "gitdir:~/code/personal/"]
      	path = .gitconfig-personal
      [includeIf "gitdir:~/code/professional/"]
      	path = .gitconfig-professional
  • In your ~/.gitconfig-personal, add your personnal user informations:

      [user]
      	email = user.personal@users.noreply.github.com     # note we use the noreply github mail
      	name = personal_username
  • In your ~/.gitconfig-professional, add your professional user informations:

      [user]
      	email = user.professional@dns.com
      	name = professional_username

Setup a GPG key

If you need to add a GPG key and bind it to a user to sign your commits, you can do so like this:

You should have GPG installed and configured like the GPG suite

  • Add the GPG key ID to your ~/.gitconfig-<PROFILE> config and enable commit signing:

    [user]
      email = your.mail@domain.com
      name = Your NAME
      signingkey = SIGNING_KEY_ID
    [commit]
      gpgsign = true
  • Make sure to register the right GPG binary in your ~/.gitconfig:

    [program]
      pgp = /path/to/your/gpg2/bin

Test your setup

  • Now each repository will use the custom user info setup depending on the top-level folder.

  • Check your settings are taken into account, for instance in ~/code/personal/ :

    $ cd ~/code/personal/
    $ git config --get user.email    	# should return user.personal@users.noreply.github.com as per the exemple
    $ git config --get user.name     	# should return personal_username as per the exemple
    $ git config --get user.signingkey	# should return the GPG key ID as configured for the user
  • Do the same for each folder you have setup.

  • You can also display and check the global git config: git config --list --global

    • Or just the local config for the repository folder you are in: git config --list
  • Done !

Roadmap

  • use git config commands instead of config file manual editions (editing config directly is kind of dangerous but is more understandable, maybe keep it like this ?)
@slmg

This comment has been minimized.

Copy link

@slmg slmg commented May 20, 2020

Thanks for the gist. To partially address your roadmap, here's how to deal with the includeIf section only using git config commands:

git config --file=.gitconfig-personal --add user.name personal_username
git config --file=.gitconfig-personal --add user.email user.personal@users.noreply.github.com

git config --global --add includeif.gitdir:~/code/personal/.path .gitconfig-personal
@bgauduch

This comment has been minimized.

Copy link
Owner Author

@bgauduch bgauduch commented May 26, 2020

Thanks for the tip @slmg !

@d3v-nu11

This comment has been minimized.

Copy link

@d3v-nu11 d3v-nu11 commented Oct 8, 2020

Thanks for the gist. To partially address your roadmap, here's how to deal with the includeIf section only using git config commands:

git config --file=.gitconfig-personal --add user.name personal_username
git config --file=.gitconfig-personal --add user.email user.personal@users.noreply.github.com

git config --global --add includeif.gitdir:~/code/personal/.path .gitconfig-personal

I dont see includeif as a option under git config --gloabl --add , am i missing something? running git 2.28.0

@slmg

This comment has been minimized.

Copy link

@slmg slmg commented Oct 8, 2020

It is shown under git config.

> git --version
git version 2.25.1

> git config --help | grep includeIf
       The include and includeIf sections allow you to include config directives from another source. These sections behave identically to each other with the exception
       that includeIf sections may be ignored if their condition does not evaluate to true; see "Conditional includes" below.
       You can include a config file from another by setting the special include.path (or includeIf.*.path) variable to the name of the file to be included. The
       You can include a config file from another conditionally by setting a includeIf.<condition>.path variable to the name of the file to be included.
           [includeIf "gitdir:/path/to/foo/.git"]
           [includeIf "gitdir:/path/to/group/"]
           [includeIf "gitdir:~/to/group/"]
           [includeIf "gitdir:/path/to/group/"]
           [includeIf "onbranch:foo-branch"]
@slmingol

This comment has been minimized.

Copy link

@slmingol slmingol commented Dec 6, 2020

Keep in mind that every time you run git config --file=.gitconfig-personal --add user.name or whatever git config cmd it'll keep adding entries to the specified file. It's likely better to use --replace-all.

@offwork

This comment has been minimized.

Copy link

@offwork offwork commented Dec 6, 2020

I repeated the steps over and over but the name and mail are not recognized by git. I had already tried something similar to this before but failed. I can try if I see something different.

@igorbrites

This comment has been minimized.

Copy link

@igorbrites igorbrites commented Jan 4, 2021

I repeated the steps over and over but the name and mail are not recognized by git. I had already tried something similar to this before but failed. I can try if I see something different.

I was having the same problem, but I realised that the path without the trailing / does not work. The commands from @slmg saved the day! Thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment