Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bgauduch/06a8c4ec2fec8fef6354afe94358c89e to your computer and use it in GitHub Desktop.
Save bgauduch/06a8c4ec2fec8fef6354afe94358c89e to your computer and use it in GitHub Desktop.
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 = [email protected]     # note we use the noreply github mail
      	name = personal_username
  • In your ~/.gitconfig-professional, add your professional user informations:

      [user]
      	email = [email protected]
      	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 = [email protected]
      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 [email protected] 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 ?)
@MGREMY
Copy link

MGREMY commented Apr 24, 2024

Thanks man ! Note that git config --get xxxx.xxxx works only when you are inside a repository, otherwise it doesn't show anything 👍

@offwork
Copy link

offwork commented Apr 25, 2024

Hi there!

Simple solution for Mac and fish-shell users like me:
After the ssh keys are created, run the agent command for fish:

eval $(ssh-agent -c)

and then install the ssh keys on the mac keychain:
ssh-add --apple-load-keychain -A ~/.ssh/github_personal
ssh-add --apple-load-keychain -A ~/.ssh/bitbucket_work

and then install the ssh keys on the mac keychain.

Screenshot 2024-04-25 at 15 42 29

@shellheim
Copy link

I originally had a problem with using two hosts and when I signed my commits, the signature would be invalid on the web UI because my global git email was set to github only. What I wanted to do was figure out a way to automatically change the user.email variable to the respective noreply addresses.

That can be done using the IncludeIf directive, just have to use the right globbing pattern.
My config is like this :

[includeIf "hasconfig:remote.*.url:**github.com:*/*.git"]
	path = github_config 

[includeIf "hasconfig:remote.*.url:**codeberg.org:*/*.git"]
	path = codeberg_config

Where github_config and codeberg_config are files with their respective emails. The globbing pattern is just :

**example.com:*/*.git

for ssh remote urls.

@amaury-d
Copy link

amaury-d commented Sep 2, 2024

@shellheim nice tip 👍

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