Skip to content

Instantly share code, notes, and snippets.

@lopugit
Created February 21, 2024 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lopugit/9e00dfee651269c20610d125fe51c0db to your computer and use it in GitHub Desktop.
Save lopugit/9e00dfee651269c20610d125fe51c0db to your computer and use it in GitHub Desktop.
Multi SSH KEY config for same hosts based on directory
Finally, multi ssh key config for same host git repo's :exploding_head:
https://superuser.com/questions/366649/ssh-config-same-host-but-different-keys-and-usernames
Alternative Git configuration to automatically switch accounts depending on repo path:
Create a file ~/.config/git/config.user1 containing:
[url "github_username1:"]
insteadOf = git@github.com:
Create a config.user2 file that's the same except with "github_username2".
In your main ~/.config/git/config file, tell Git to "include" one of the two files depending on which directory you're at:
[includeIf "gitdir:~/projects/"]
path = ~/.config/git/config.user1
[includeIf "gitdir:~/src/work/"]
path = ~/.config/git/config.user2
Now, whenever you're at ~/src/work/, cloning anything from git@github.com:[etc] will automatically rewrite the URL to github_username2:[etc].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment