Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sokardys/9a4543d5bbb572ed5512d702a65852fa to your computer and use it in GitHub Desktop.

Select an option

Save sokardys/9a4543d5bbb572ed5512d702a65852fa 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/id_ed25519
  IdentitiesOnly=yes

Host <alias>
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_<profile>
  IdentitiesOnly=yes

/!\ Must modify the uris when using the git commands (clone, remote, ...) to use the certificate of the desired user. The certificate is what identifies the github user to use.

  • Cloning a project:
// Original command
git clone [email protected]:sokardys/sample-project.git

// Use instead. It is not necessary to put the `git` user, it is indicated in `~/.ssh/config`
git clone <alias>:sokardys/sample-project.git
  • Adding remote origin
// Original command
git remote add origin [email protected]:sokardys/sample-project.git

// Use instead. 
git remote add origin <alias>:sokardys/sample-project.git

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, include de global [user] block and add [includeIf] blocks according to your needs :
# Global user config settings
[user]
  name = Javier Ortiz
  email = [email protected]

# Overwrite with another settings when you are in other path.
# Change the directory as you like
[includeIf "gitdir:~/projects/<organization>/"]
  	path = .gitconfig-<organization>
  • In your ~/.gitconfig-<organization>, add new [user] information to overwrite:
# Customized user to use from a specific directory
[user]
  name = <NAME>
  email = <EMAIL>
  • To check the configuration you can use the command git config --list. It will look something like this from ~/projects/<organization>/<project-name>:
user.name=Javier Ortiz
[email protected]
includeif.gitdir:~/projects/<organization>/.path=.gitconfig-<organization>
user.name=<NAME>
user.email=<EMAIL>

/!\ The order is important, the settings are overwritten from top to bottom.

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