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!
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).
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.
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.
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.
- Solved: Build Errors Not Showning in VS 2022 - 21st November 2024
- How To: Configure VSCode to Trust Self-Signed Certs - 16th August 2024
- Solved: GitHub Actions – HTTP 403: Resource not accessible by integration - 13th June 2024
26th July 2021 at 8:52 pm
I am unable to find a way to pass the personal access token while cloning the private repository. While cloning, I am not getting option to enter my username or password. I have even tried to provide the usename:password@github.com format in the HTTPs URL while cloning the repository. FYI, my token is a successfully created and authenticated with the private repository.
26th July 2021 at 9:32 pm
I suspect the credentials have been cached on your local machine, so you’ll need to clear the cache. You should then be prompted for the credentials once again. If you are using Windows then the credentials will be stored in the Windows Credential Manager by default.
19th September 2021 at 7:29 am
Worked. Thanks
1st April 2022 at 6:30 pm
using the following format worked for me: git clone https://@github.com//