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.
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.
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
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
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!