gitgithubhttpspermission-denied

Trying pushing to remote repository, permission denied and received error 403


I created a local repository on my laptop (MacOS) and started a project. After working on it for some time I wanted to sync it with an empty remote repository I made on GitHub. I tried to link the repositories using HTTPS and GitHub access tokens. First I added and set up the remote like this in Shell:

$ git remote add origin https://github.com/[username]/[github-repository-name].git

Then I generated a fine-grained access token and entered it in a command like this:

$ git remote set-url origin https://[github-token]@github.com/[username]/[github-repository-name].git

But when I tried pushing my local changes, I received an error in this format:

remote: Permission to [username]/[github-repository-name].git denied to [username]

fatal: unable to access 'https://github.com/[username]/[github-repository-name].git/': The requested URL returned error: 403

I checked that my remote repository had public access, tried created another fine-grained access token and made sure to click "Public Repositories (read-only)" under "Repository access" and repeating the steps I made in the beginning and double-checked that the remote url was correct. However I received the same output again.

I tried google searching my problem but after some time, I'm still stuck. I know I can try using SSH instead but I want to figure out what went wrong so I know how to avoid it in the future. I'm new to using GitHub so I appreciate any explanation or tips on solving this problem.


Solution

  • "Public Repositories (read-only)" means that you allow your token to represent you for requests that are read-only operations on public repository. Pushing commits is not a read-only operation.

    You need to chose either "all repositories" or "selected repositories". Up to you which repositories you want this specific token to have access to, but you need to pick either of these options for write operations.

    In a fine-grained token you need to select which category of write operations this token is allowed to be used for. I think what you want to start with is to set "Content" to "read and write". But often you want to give your Git client more access than that.

    Since you are new to this, start with the most restrictive access and then carefully give more privileges to your tokens as you learn more about their usage.