Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbpzen/c6a448c5a3b7700b8344d8b1171dafcc to your computer and use it in GitHub Desktop.
Save jbpzen/c6a448c5a3b7700b8344d8b1171dafcc to your computer and use it in GitHub Desktop.

Setup multiple git identities & git user informations

/!\ Be very carrefull in your setup : any misconfiguration make all the git config to fail silently !

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
      Host gitlab.com
      	Hostname gitlab.com
      	User git
      	IdentityFile ~/.ssh/gitlab_private_key

Setup dynamic git user mail & name depending on folder

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

  • 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
  • Done !
  • Now each repository will use custom user info depending on the top-level folder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment