How to Properly Manage Multiple SSH Keys for Git on Your Local Environment

Image for post
Image for post
Photo by Yancy Min on Unsplash
  • Advanced Use Case: multiple deploy keys for multiple repositories

Basic Use Case: deploy keys for a single repository

To avoid entering your credentials each time you act on your Git repository, you want to manage deploy keys for your different environments. For this, you will need an ssh key.

ssh-keygen -t ed25519 -C "email@example.com"
cd ~/.ssh/
  • id_ed25519
git fetch
Image for post
Image for post
Deploy Keys on Github.com

Advanced Use Case: multiple deploy keys for multiple repositories

Git as a way to configure multiple deploy keys with patterns to apply them. The first thing you will need to do is create a config file.

nano ~/.ssh/config
Host project1.github.com
Hostname github.com
IdentityFile ~/.ssh/key_project1
Host project2.github.com
Hostname github.com
IdentityFile ~/.ssh/key_project2
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
[remote "origin"]
url = git@project1.github.com:Cach54/project1.git
fetch = +refs/heads/*:refs/remotes/origin/*
git fetch

Troubleshooting

I am managing a lot of different projects, on different repositories, and for different companies. So I am often confronted with changes in my config file and sometimes it can become messy.

ssh-add -D
ssh -v git@github.com

What’s Next

  • Dockerize your keys to use Git inside your Docker containers without having to manually duplicate your configuration
  • Replicate the same process for your Staging and Production environment

Freelance IT Project Manager | Writer for The Startup and DataDrivenInvestor | 📍Paris

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store