How to Use SSH and Personal Access Tokens with GitHub

Satyam Pund
4 min readJul 18, 2024

--

When working with GitHub, you often need to interact with repositories using Git commands. Two secure methods to authenticate and interact with your repositories are using Personal Access Tokens (PATs) and SSH keys. This guide will walk you through both methods step by step.

1. Using Personal Access Tokens

What is a Personal Access Token?

A Personal Access Token (PAT) is a secure way to authenticate with GitHub when using Git. It acts as a password and provides access to your repositories.

How to Generate a Personal Access Token

  1. Log in to GitHub and navigate to your profile settings.

2. Go to Developer settings.

3. Select Personal access tokens and then click on Tokens(classic).

4. Click on Generate new token and then click on Generate new token (classic).

5. Select the scopes or permissions you’d like to grant this token.

6. Generate the token and copy it. It will look something like this: ghp_16characterlongtoken.

Note: This is the only time you will be able to see the token.

Cloning a Repository with a Personal Access Token

To clone a repository using a PAT:

  • Open your terminal or command prompt.
  • Run the following command:
git clone https://<token>@github.com/<gitHubUserName>/<repo>.git

Replace <token>, <gitHubUserName>, and <repo> with your actual token, GitHub username, and repository name. For example:

git clone https://ghp_16characterlongtoken@github.com/johndoe/myrepo.git

After cloning the repository using a Personal Access Token (PAT), you can commit and push code to the repository. The PAT will be used for authentication, so you don’t need to enter your username and password each time.

Managing the Remote Origin

There are some cases where you may need to manage the remote origin, such as switching from HTTPS to HTTPS with PAT, changing the PAT, or updating the repository URL.

Scenario 1: Switching to PAT Authentication

Suppose you cloned a repository with the URL https://github.com/johndoe/myrepo.git using your GitHub username and password. You want to switch to using a PAT instead.

  1. Remove the current origin:
git remote remove origin

2. Add the origin with the PAT:

git remote add origin https://ghp_16characterlongtoken@github.com/johndoe/myrepo.git

Scenario 2: Updating the PAT

If you have generated a new PAT because the old one expired, you will need to update the remote URL.

  1. Set the new URL with the new PAT:
git remote set-url origin https://ghp_new16characterlongtoken@github.com/johndoe/myrepo.git

Scenario 3: Repository URL Change

If your repository’s URL has changed, for example, it has been transferred to a different organization or user.

  1. Set the new URL:
git remote set-url origin https://ghp_16characterlongtoken@github.com/newusername/newrepo.git

2. Using SSH

What is SSH?

SSH (Secure Shell) is a protocol that provides a secure way to access remote computers. When using GitHub, SSH keys can be used for secure access to your repositories.

Generating SSH Keys

To generate SSH keys:

  1. Open a terminal.
  2. Generate the key pair with:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Adding SSH Key to GitHub

  1. Copy the SSH key to your clipboard
cat ~/.ssh/id_rsa.pub

2. Log in to GitHub and navigate to your profile settings.

3. Go to SSH and GPG keys.

4. Click on New SSH key, give it a title, and paste your key.

Cloning a Repository with SSH

To clone a repository using SSH:

  1. Open your terminal.
  2. Run the following command:
git clone git@github.com:<gitHubUserName>/<repo>.git

Replace <gitHubUserName> and <repo> with your actual GitHub username and repository name.

Example:

git clone git@github.com:johndoe/myrepo.git

After cloning the repository using SSH, you can commit and push code to the repository. The SSH key will be used for authentication, so you don’t need to enter your username and password each time.

Using Personal Access Tokens and SSH keys provides secure ways to manage your GitHub repositories. This guide has covered how to generate and use PATs and SSH keys for cloning repositories and managing remote origins. Choose the method that best suits your workflow and enhances your security practices.

--

--