As a developer, after getting a new laptop there are sequence of setups and configuration one needs to do to keep the computer development ready. As part of it, configuring git is very important.
Git provides “Connect with Secure Shell Protocol (SSH)” as a convenient way to commit file to repository without the need to enter user credentials (username / password) every time. On a new computer you generally need to generate a new SSH key, setup your identity (user and password), configure default text editor and finally setup gitconfig to switch between multiple users.
- Generate a new SSH key and adding it to the ssh-agent following the Git Guide
# These commands are valid as of Nov 7th, 2022 - Always uses the Git Guide to get the latest commands and steps. I will try to keep this updated as accurate as possible.
# Generating a new SSH key
ssh-keygen -t ed25519 -C "pkasi@raptor.in"
(use the default file path to save the key)
(use a passphrase that you won't forget)
# Adding your SSH key to the ssh-agent
eval "$(ssh-agent -s)"
open ~/.ssh/config
(replace with this config)
Host *.github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
# Add your SSH private key to the ssh-agent and store in keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
- Configure your global identity
git config --global user.name "Prabhakar Kasi"
git config --global user.email pkasi@raptor.in
- Configure the default text editor
git config --global core.editor vi
- (Optional) Setup multiple ssh key. This is handy if you want to isolate work and personal git environment. Below I have captured steps for 1 office-git and 2 personal-github accounts.
# Generating a new SSH key - Personal Email1
ssh-keygen -t ed25519 -C "pkasi@*****.com"
(use different file name to save the key, as we don't want to overwrite previously created file)
(use a passphrase that you won't forget)
# Add your SSH private key to the ssh-agent and store in keychain
ssh-add --apple-use-keychain ~/.ssh/p1_ed25519
# Repeat above two commands for personal email2
# For one office-git and two personal account the ~/.ssh/config looks like this
Host git.office-pkasi
HostName git.***.com #hostname of office-instance goes here
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Host github-pkasi
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/pkasi_ed25519
Host github-raptor
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/raptor_ed25519
- (Optional) Switching between different ssh key. Since office-git instance and free-personal-github.com instance has different HostName (urls) simplying adding office-ssh key would and adding free-personal instance would work by running these two commands.
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add --apple-use-keychain ~/.ssh/pkasi_ed25519
But if we want to switch between two different free-personal-github.com account we need remove the existing private that has been added already
# To user raptor account
# Delete specific identity and then add the required identity
ssh-add -d ~/.ssh/pkasi_ed25519
ssh-add --apple-use-keychain ~/.ssh/raptor_ed25519
# To user pkasi personal account
# Delete specific identity and then add the required identity
ssh-add -d ~/.ssh/raptor_ed25519
ssh-add --apple-use-keychain ~/.ssh/pkasi_ed25519
# Use -D to delete all identities from the agent
ssh-add -D
- Finally we need to adding the public key to git account in order for git commands to work with proper authentication. Follow this Git guide
# Copy the public key for office-account
pbcopy < ~/.ssh/id_ed25519.pub
# Open git.office.com
Go to Settings -> SSH and GPG keys -> New SSH key
# Repeat above two steps for each accounts in github.com
- Verify if everything works fine by cloning some repo and push changes
git clone git@github.com:user/repo-name.git
cd repo-name
git fetch origin master
git rebase origin/master
git push -f origin master (This command will not succeed if the ssh identity is not properly configured)
Reference
- https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
- https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
Error
Updated on May 7th, 2024
Fix Error - Error connecting to agent: No such file or directory
# When trying to add your SSH private key to the ssh-agent and store your passphrase in the keychain. You might get the following error.
ssh-add --apple-use-keychain ~/.ssh/pkasi_ed25519
> Error connecting to agent: No such file or directory
# When trying to remove all SSH private key added the ssh-agent. You might get the following error.
ssh-add -D
> Error connecting to agent: No such file or directory
# Fix => Start the ssh-agent in the background
eval "$(ssh-agent -s)"