DEV Community

Cover image for Configuring Git SSH Keys for Multiple Accounts and Workspaces
Orhun Özer
Orhun Özer

Posted on • Updated on

Configuring Git SSH Keys for Multiple Accounts and Workspaces

You can have different keys based on different domains. The following code is an example of how you can configure your SSH keys for different accounts on GitHub and Bitbucket.

code ~/.ssh/config

# Personal account
Host github.com
   HostName github.com
   User git
   UseKeychain yes 
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_ed25519

# Work account-1
Host bitbucket.org    
   HostName bitbucket.org
   User git
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Multiple git configs

If you want to have multiple git configs one of the easiest ways to achieve this is having a separate workspaces and gitconfigs respectively.

In the context git, a workspace refers to a directory or folder on your computer where you store your work-related projects and files.

By using the includeIf directive in your .gitconfig file, you can tell Git to use a specific configuration file depending on which workspace you're currently in.

In case you don't have a gitconfig;

touch .gitconfig 
code .gitconfig
Enter fullscreen mode Exit fullscreen mode

Inside of .gitconfig separate config files based on path

[includeIf "gitdir:/Users/your_username/workspace-company/"]
path = ~/.gitconfig-company
[includeIf "gitdir:/Users/your_username/workspace-personal/"]
path = ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

then in each config you’ll have different setup.

.gitconfig-company

[user]
 name = Orhun Ozer
 email = orhun.ozer@company.com
Enter fullscreen mode Exit fullscreen mode

.gitconfig-personal

[user]
 name = HelluvaDevelopa
 email = helluva@developa.com
Enter fullscreen mode Exit fullscreen mode

Top comments (0)