Cloning a Private GitHub Repository Using HTTPS

Daniel Akudbilla - Mar 3 - - Dev Community

If you're here, you're either struggling to clone a private GitHub repository or simply trying to learn how. Let's dive straight into the solution.
You have encountered this error
Cloning into 'repo_name'...
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/username/repo_name.git/': The requested URL returned error: 403

Step 1: Generate a Personal Access Token (PAT)

To clone a private repository using HTTPS, you need a Personal Access Token (PAT) instead of a password. Follow these steps to generate one:

1. Log in to GitHub

  • Go to GitHub and sign in to your account.
  • Click on your profile picture in the top-right corner.
  • Select Settings.

2. Navigate to Developer Settings

  • Scroll down and click on Developer settings (found in the left sidebar).
  • Under Personal access tokens, select Fine-grained tokens.
  • Click Generate new token.

access token image

3. Configure the Token

  • Name the token (e.g., GitHub Repo Access).
  • Select the resource owner: Choose your GitHub username (or your organization if cloning an organization repo).
  • Set an expiration date: Choose an appropriate duration for security purposes.
  • Repository access:
    • Select All repositories (if you want access to all your repos).
    • Recommended: Select only the specific repositories you need access to.
  • Permissions:
    • Under Repository permissions, find Contents and set it to Read and Write.
    • Find Administration and set it to Read and Write.
    • Grant additional permissions as needed.

Permissions options to be selected

4. Generate and Copy the Token

  • Click Generate Token.
  • Important: Copy the generated token and store it securely, as it will not be shown again.

Step 2: Clone the Private Repository

With your Personal Access Token (PAT) ready, follow these steps to clone your private repository:

1. Open Your Terminal

On Windows, use Git Bash or Command Prompt.
On Mac/Linux, use the Terminal.

2. Run the Clone Command

Use the following command to clone your repository:

git clone https://USERNAME:TOKEN@github.com/USERNAME/REPOSITORY_NAME.git
Enter fullscreen mode Exit fullscreen mode

Replace:

  • USERNAME → Your GitHub username
  • TOKEN → Your generated Personal Access Token
  • REPOSITORY_NAME → The name of your repository

Example:

git clone https://johnDoe:ghp_ABC123xyz@github.com/johnDoe/my-private-repo.git
Enter fullscreen mode Exit fullscreen mode

If everything is correct, your repository should start cloning successfully.


Final Thoughts

I personally struggled with this issue for a while and decided to share my findings after thorough research. I hope this guide helps you avoid the same frustration. If you found this useful, share it with others who might need it!

🚀 Happy coding!

.