All articles
git

Use GitHub with SSH - Complete guide including VSCode setup

Share this article

Share on LinkedIn Share on X (formerly Twitter)

SSH keys provide a secure, password-free way to interact with GitHub and other Git services (like GitLab/Bitbucket). This professional approach eliminates the need for personal access tokens or passwords in your command-line workflow. Let's set it up step by step.

Before You Start

  1. Install Git: Download Git for your OS
  2. Use Terminal:
    • Windows: Git Bash (installed with Git)
    • Mac/Linux: Built-in Terminal
  3. Delete old tokens: Remove existing personal access tokens at GitHub Tokens Settings

Step 1: Configure Your Git Identity

Set your commit author name and email (use the same email as your GitHub account):

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Verify configuration:

git config --global user.name  # Should return your name
git config --global user.email # Should return your email

💡 Recommended settings (prevents common issues):

git config --global core.autocrlf true    # Handles line endings
git config --global core.ignorecase true  # Avoids filename case issues

Step 2: Generate SSH Key Pair

Check for existing keys:

ls -al ~/.ssh  # Lists files in .ssh directory

If you see files like id_rsa (private key) and id_rsa.pub (public key), you can reuse them. Otherwise, create new keys.

Generate new SSH key:

ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
  • Press Enter a few times to accept default locations and skip passphrase (or set a secure passphrase)
  • Windows users: Run this in Git Bash, not Command Prompt

🔐 Passphrase tip: A passphrase adds extra security but requires entry each time you use the key. For beginners, leave it empty. This is NOT same as the password that you use to login into GitHub.


Step 3: Add SSH Key to GitHub

  1. Copy your public key:

    • Windows (Git Bash):
      clip < ~/.ssh/id_rsa.pub
    • Mac:
      pbcopy < ~/.ssh/id_rsa.pub
    • Linux:
      xclip -sel clip < ~/.ssh/id_rsa.pub  # If xclip installed
      # OR manually display/copy:
      cat ~/.ssh/id_rsa.pub
  2. Add key to GitHub:

    • Go to GitHub SSH Keys Settings
    • Click New SSH Key
    • Paste key into "Key" field
    • Add descriptive title (e.g., "MacBook Pro 2023")
    • If prompted, select "Authentication Key" as the Key type
    • Click Add SSH key

Step 4: Test Your Connection

Verify SSH setup:

ssh -T git@github.com

You should see:

Hi username! You've successfully authenticated...


Step 5: Use SSH with Git Repositories

For new repositories:

Always clone using SSH (not HTTPS):

# ✅ USE THIS (SSH):
git clone git@github.com:username/repo.git
 
# ❌ AVOID THIS (will prompt for password):
git clone https://github.com/username/repo.git

For existing repositories:

Check your remote URL:

git remote -v

If it shows an HTTPS URL, switch to SSH:

git remote set-url origin git@github.com:username/repo.git

Optional: SSH Key Management

You can add your SSH key to the ssh-agent, if you don't want reenter your passphrase every time you use your SSH key. To do so:

Start the ssh-agent in the background, using the command

eval "$(ssh-agent -s)"

Run the following command to delete all existing identities from the agent

ssh-add -D

Then add the generated key to the agent

ssh-add ~/.ssh/id_rsa

To list the added keys:

ssh-add -l

You can also read GitHub's guide to SSH key and SSH agent

Troubleshooting tips:

  • Permission denied? Ensure you copied the public key (ends with .pub)
  • ~/.ssh missing? ssh-keygen creates it automatically
  • Changed GitHub email? Update email in git config and SSH key

Editor Setup: VS Code

VS Code works automatically when you have SSH keys without a passphrase. Or if your ssh-agent is enabled, and you have entered at least once your passphrase.

VSCode will use the same agent to get the passphrase when it will need it.

Troubleshooting tips:

If your VS Code does not work, try:

  • Windows: Launch VS Code from Git Bash:
    code /path/to/your/project
  • Mac/Linux: Launch from terminal app
  • Disable Git credential helper:
    git config --global credential.helper ""

Why This Matters

  • Security: SSH keys are more secure than passwords
  • Convenience: No repeated password entries
  • Compatibility: Works with all major Git providers
  • Professional: Standard practice in development teams

Troubleshooting? Post error messages in comments!


More resources:


If you have multiple github accounts, then try checking this article on Managing Multiple GitHub Accounts with SSH Keys


Comments