Overview

In this post I'll show you how you can clone a Git repository using a Personal Access Token instead of a regular password.

Personal Access Tokens are useful in a variety of situations. I recently opted to use an access token on a work laptop rather than using my ‘god mode' credentials. That way, if I needed to hand the laptop back to my employer I could easily revoke the access.

Enough talk … lets get to it!

GitHub Icon

Create a Token

First thing we need to do is create a Personal Access Token through the Github online portal.

1. Log into you GitHub account

2. Click your profile picture in the right hand menu and then navigate to SETTINGS > DEVELOPER SETTINGS within GitHub (or click this link to go straight there).

GitHub Settings Screenshot

3. Click ‘Generate New Token' to create a new token.

Name the token appropriately so you can identify it later on (if needed) and select the appropriate scope. I'd suggest you select the Repo – Full Control scope.

IMPORTANT: Make note of the token because once you close the window you won't be able to view the token again!

4. Name the token appropriately so you can identify it later on (if needed) and finally, select the appropriate scope. Note: At a minimum for cloning a repo I suggest you select the Repo – Full Control scope.

GitHub Create New Personal Access Token
GitHub Create New Personal Access Token

Clone Repo With Your Token

Now lets clone a repo using your newly generated Personal Access Token. You'll notice that I'm using the token instead of the ordinary account password.


$ git clone https://github.com/fakeuser/fake-repo.git
Username: <your_username>
Password: <your_personal_access_token>

Common Issues

While Cloning a Private Repo using a Personal Access Token I encountered the below error:

ERROR: Repository not found.

The cause of the issue is lack of privileges. Make sure you have granted ‘full control' access rights to your Personal Access Token, anything less did not work for me.

Git Personal Access Token Screenshot

Final Thoughts

I hope you've found this post useful. If you have any other tips to share, please post them below to help others out there.

Shane Bartholomeusz
Latest posts by Shane Bartholomeusz (see all)