Data report"State of code review 2024" is now liveRead the full report

How to use SSH with Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide provides a detailed look into how to use SSH with Git, including creating SSH keys, adding them to your GitHub account, and cloning repositories using SSH.

SSH, or Secure Shell, is a cryptographic network protocol that enables secure communication over an unsecured network. It provides a secure channel over an unsecured network in a client-server architecture, offering strong authentication and encrypted data communications. In the context of Git, SSH is crucial for securely pushing to and pulling from repositories without having to enter your username and password each time.

To create a new SSH key use the following command:

Terminal
ssh-keygen -t ed25519 -C "your_email@example.com"

Replace "your_email@example.com" with your email address. This command generates a new SSH key using the Ed25519 algorithm, which is considered secure and efficient.

When you run the ssh-keygen command, it will ask you to choose a location to save the key. You can press Enter to accept the default location (~/.ssh/id_ed25519).

After specifying the file location, you’ll be prompted to enter a passphrase. This adds an additional layer of security.

Adding your SSH key to the ssh-agent can help you manage your SSH keys. The ssh-agent is a program that holds private keys used for public key authentication (SSH) so that the user is not required to re-enter the passphrase every time. Here's how to add your SSH key to the ssh-agent:

Before you can add your SSH key to the ssh-agent, you need to ensure that it's running. You can start it with the following command:

Terminal
eval "$(ssh-agent -s)"

This command will start the ssh-agent in the background and print the agent process ID. On some systems, the ssh-agent might already be running or it might be started automatically at login.

To add your SSH private key to the ssh-agent, use the ssh-add command followed by the path to your private key file. If you created your key with a default name and location, you can simply use:

Terminal
ssh-add ~/.ssh/id_rsa

If your private key file has a different name or is located in a different directory, you'll need to specify the full path to the file:

Terminal
ssh-add ~/.ssh/your-custom-key-name

If your SSH key was created with a passphrase (which is recommended for additional security), you will be prompted to enter it when you add the key to the ssh-agent. Once entered, the ssh-agent will store the unlocked key in memory and manage it for subsequent authentications, so you won't need to enter the passphrase again during the current session.

  • Check loaded SSH keys: You can check which keys are currently managed by the ssh-agent with the command:
    Terminal
    ssh-add -l
  • Remove keys: If you need to remove keys from the ssh-agent, you can use:
    Terminal
    ssh-add -d path/to/key
    To remove all keys, you can use:
    Terminal
    ssh-add -D

To copy the SSH public key to your clipboard, use the following command:

Terminal
cat ~/.ssh/id_ed25519.pub | pbcopy

This command reads your public key and copies it into your clipboard.

  1. Log in to your GitHub account.
  2. Navigate to the SSH and GPG keys settings page.
  3. Click New SSH key or Add SSH key.
  4. Paste your key into the "Key" field.
  5. Add a descriptive title.
  6. Click Add SSH key.

Navigate to the repository on GitHub, click on Clone or download, and switch to Clone with SSH. Copy the SSH URL provided.

To clone a repository using SSH, use the following command:

Terminal
git clone git@github.com:<REPO_OWNER>/<REPO_NAME>.git

Replace <REPO_OWNER> and <REPO_NAME> with the owner and name of the repo you'd like to clone.

For further reading see the official GitHub documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2