Skip to content

Instantly share code, notes, and snippets.

@portablejim
Last active July 10, 2023 08:08
Show Gist options
  • Save portablejim/77bbf476f47e54630011fb54819f8124 to your computer and use it in GitHub Desktop.
Save portablejim/77bbf476f47e54630011fb54819f8124 to your computer and use it in GitHub Desktop.
Use SSH with github instead of HTTPS with company Org repositories.

If you have a Organization with SSO turned on but want to use SSH keys instead of HTTPS auth.

(for example the

Step 1: Generate a different SSH key to use with the organisation. For example, the private and public key are in ~/.ssh/companykey and ~/.ssh/companykey.pub.

Step 2: Add the key and authorise the key with SSO - in https://github.com/settings/keys use "New SSH Key" to add the key, then use 'Configure SSO' to authorise it for the target organisation.

Step 3: Create a SSH configuration for the different key. Add the following to ~/.ssh/config.

Host github.com-company
  User git
  Hostname github.com
  IdentityFile ~/.ssh/companykey
  IdentitiesOnly yes

Step 4: Add a URL replacement rule using the following command

git config --global url."git@github.com-company:Company/".insteadOf "https://github.com/Company/"

The git config file (at ~/.gitconfig) should now contain

[url "git@github.com-tt:Company/"]
	insteadOf = https://github.com/Company/

if you want normal git links to work as well, add a second insteadOf line for the git version of the URL. The section would now read

[url "git@github.com-company:Company/"]
	insteadOf = https://github.com/Company/
	insteadOf = git@github.com:Company/

It now should work and use the correct key.

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