Skip to content

Instantly share code, notes, and snippets.

@digaji
Last active March 7, 2024 09:53
Show Gist options
  • Save digaji/2352c03c7bdf342fa9c167ce600ee179 to your computer and use it in GitHub Desktop.
Save digaji/2352c03c7bdf342fa9c167ce600ee179 to your computer and use it in GitHub Desktop.
Multi Git Account Handling: SSH and gitconfig Setup

Multi Git Account Handling: SSH and gitconfig Setup

Original Medium Article link: Here

SSH

Create SSH Keys for all accounts

ssh-keygen -t ed25519 -C example_personal_email@example.com -f ed25519_personal
ssh-keygen -t ed25519 -C example_work_email@example.com -f ed25519_work_gl
ssh-keygen -t ed25519 -C example_work_email@example.com -f ed25519_work_gh

Start SSH Agent in Background and add all Private keys

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/ed25519_personal
ssh-add ~/.ssh/ed25519_work_gl
ssh-add ~/.ssh/ed25519_work_gh

Optional: Add to start-up script to automate ssh-agent

# ssh-agent
{ eval `ssh-agent -s` } &>/dev/null
{ ssh-add ~/.ssh/id_ed25519 } &>/dev/null
{ ssh-add ~/.ssh/id_ed25519_personal } &>/dev/null
{ ssh-add ~/.ssh/id_ed25519_gh } &>/dev/null

Add SSH Public keys to GitHub and GitLab

Copy Public key to clipboard

# Mac
pbcopy < ~/.ssh/ed25519_personal.pub

# Windows
clip < ~/.ssh/ed25519_personal.pub

GitHub and GitLab documentation

For GitHub, navigate over to User > Settings > SSH and GPG keys > New SSH key

For GitLab, navigate over to User > Preferences > SSH Keys

Manage config file and entries

cd ~/.ssh/
touch config
nvim config		// Opens the file in Neovim, feel free to use your preferred editor

Add relevant entries to config file

# Personal account
Host github.com
	Hostname github.com
	User git
	IdentityFile ~/.ssh/ed25519_personal

# Work GitLab account
Host gitlab.com
	Hostname gitlab.com
	User git
	IdentityFile ~/.ssh/ed25519_work_gl

# Work GitHub account
Host github.com-work
	Hostname github.com
	User git
	IdentityFile ~/.ssh/ed25519_work_gh

Verify SSH connections

ssh -T git@github.com

ssh -T git@gitlab.com

ssh -T git@github.com-work

.gitconfig

[user]
    name = <your_username>
    email = <your_email>

[includeIf "gitdir:~/GitLabWorkPath/"]
    path = ~/GitLabWorkConfigPath

[includeIf "gitdir:~/GitHubWorkPath/"]
    path = ~/GitHubWorkConfigPath

For each config path

[user]
    name = <your_work_username>
    email = <your_work_email>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment