This guide walks you through configuring SSH authentication with Git CLI for secure repository access.
- Git installed on your system
- Access to a terminal/command line
- A Git hosting account (GitHub, GitLab, or Bitbucket)
First, check if you already have SSH keys:
ls -al ~/.sshLook for files like id_rsa.pub, id_ed25519.pub, or id_ecdsa.pub.
If you don't have an SSH key, generate one using Ed25519 (recommended):
ssh-keygen -t ed25519 -C "your_email@example.com"For legacy systems that don't support Ed25519, use RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"During key generation:
- Press Enter to accept the default file location (
~/.ssh/id_ed25519) - Optionally set a passphrase for additional security
- Press Enter again to confirm the passphrase
Start the SSH agent in the background:
eval "$(ssh-agent -s)"You should see output like: Agent pid 12345
Add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519Note: If you generated an RSA key, use ~/.ssh/id_rsa instead.
Display your public key:
cat ~/.ssh/id_ed25519.pubCopy the entire output (it should start with ssh-ed25519 or ssh-rsa).
Alternative method (copies directly to clipboard):
- macOS:
pbcopy < ~/.ssh/id_ed25519.pub - Linux (with xclip):
xclip -selection clipboard < ~/.ssh/id_ed25519.pub - Windows (Git Bash):
clip < ~/.ssh/id_ed25519.pub
- Go to GitHub Settings
- Click SSH and GPG keys → New SSH key
- Add a descriptive title (e.g., "Work Laptop")
- Paste your public key in the "Key" field
- Click Add SSH key
- Go to GitLab Preferences
- Click SSH Keys
- Paste your public key in the "Key" field
- Add a title and optional expiration date
- Click Add key
- Go to Bitbucket Personal Settings
- Click SSH keys → Add key
- Add a label
- Paste your public key
- Click Add key
Verify that your SSH key is working:
GitHub:
ssh -T git@github.comGitLab:
ssh -T git@gitlab.comBitbucket:
ssh -T git@bitbucket.orgYou should see a success message like: Hi username! You've successfully authenticated...
git clone git@github.com:username/repository.gitCheck your current remote URL:
git remote -vChange to SSH:
git remote set-url origin git@github.com:username/repository.gitVerify the change:
git remote -vIf you get a "Permission denied" error:
- Verify your SSH key is added to the agent:
ssh-add -l - Check that you've added the correct public key to your Git provider
- Ensure you're using the correct SSH URL format
- Try adding your key again:
ssh-add ~/.ssh/id_ed25519
If the SSH agent isn't running, start it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519If you have multiple SSH keys, specify which one to use:
ssh-add -D # Remove all keys
ssh-add ~/.ssh/id_ed25519 # Add specific keyCreate or edit ~/.ssh/config to manage multiple keys or customize settings:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
- Use a strong passphrase for your SSH key
- Never share your private key (
id_ed25519orid_rsa) - Only share your public key (
.pubfiles) - Regularly rotate your SSH keys
- Use different keys for different services if needed
- Remove old or unused SSH keys from your Git provider
License: MIT
Last Updated: November 2025