DEV Community

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

Posted on • Edited on

1

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

Resources freecodecamp

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay