91

I have the error message:

remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/...../...../': 
The requested URL returned error: 403

My friend invited me to his repository, and I used his personal token while cloning it.
But it says the above error.

I also tried with my own token but it says the same.

I also faced this when I created my own repository and was making initial commit and push.
This issue has grown ever since Git*Hub has brought token authentication into account.

How can I get past this?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Bisheshwor Neupane
  • 919
  • 1
  • 6
  • 4

21 Answers21

49

Try and recreate a PAT(Personal Access Token) with, as scope, the repo ones.
And, for testing, chose an expiration date "No Expiration", to be sure it remains valid.
(Note: Since Oct. 2022, you now have fine-grained personal access tokens, which must have expiration date.)
(gdvalderrama adds in the comments: The max expiration date is 1 year and has to be manually set)

That token should start with ghp_...: it should then authenticate you properly, allowing you to clone the repository, and push back to it.

Note: a token is akin to a password (but can easily be revoked/regenerated), so you should not use any other tokens but your own.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    I tried multiple access tokens and they wouldn't work, then I finally decided to set the main "repo" scope and it finally worked. So thanks. – nonzaprej Oct 21 '22 at 13:45
  • Using expiration date "never" is not really possible, last time I did this. – Sybille Peters Dec 08 '22 at 15:12
  • 1
    @SybillePeters True, this is called "No Expiration" now. Only for "classic" token. Fine-grained tokens *must* have expiration date. I have updated the answer accordingly. – VonC Dec 08 '22 at 15:41
  • The max expiration date is 1 year and has to be manually set – gdvalderrama Dec 15 '22 at 15:58
  • @gdvalderrama Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Dec 15 '22 at 16:10
31

when you create your access token
In expiration: it should say No expiration.
In selecte scopes you mark the repo radio button.
As shown in the image below

enter image description here

Elie Eid
  • 703
  • 7
  • 11
29

I had same error , when gived persmission on github it worked
enter image description here

Halim Bezek
  • 331
  • 3
  • 5
12

In my case, I've used fine granted PAT, with all permissions, but somehow it doesn't work. After changing to the classic token, 403 disappears.

danPyk
  • 413
  • 5
  • 11
  • 3
    This worked for me after lot tries!! – Amresh Nov 24 '22 at 13:01
  • 1
    I've done the same, granted my fine-grained token all permissions possible, and I can't even do a git clone. But after trying further, it seems organisations that I created fail to clone, but direct repositories under my account are cloned successfully. – OthmanEmpire Apr 26 '23 at 12:08
  • @OthmanEmpire have you found a solution? I'm having the same problem, personal repos work but organization repos don't, even though the org is owned by me – Anthony May 15 '23 at 16:34
  • No, I am of the view that this is not possible with the current implementation of fine-grained tokens that Github offers, they are after all still in beta. I've resorted to sticking to classic tokens. Otherwise, the other solution is to create a new dedicated Github account that contains the new organisation, and make that issue the fine-grained tokens. Not too elegant but common solution for other software/products. – OthmanEmpire May 16 '23 at 20:25
11

Go to your local repository folder and find a hidden folder called ".git". find a file called "config" in the folder as attached below.
enter image description here

You need to change the url = https://github.com/... to SSH url that can find from GitHub repository(on git hub Web portal) cone menu as below picture.

enter image description here

This is what the config file looks like, after the change of the url. enter image description here

This solved my issue. I belive this will help.

  • 2
    this did not work for me. – bim Jul 21 '22 at 08:51
  • On Windows, I ended up on this well known issue: [git clone pull continually freezing](https://stackoverflow.com/questions/33240137/git-clone-pull-continually-freezing-at-store-key-in-cache) (find a solution there) – PythoNic Aug 12 '22 at 23:03
  • this works only if you have an ssh key associated with your github account – apr_dev Oct 09 '22 at 22:23
11

This is how I solved, step by step

  1. Click on your Profile Icon (top-right on Github website)
  2. Settings
  3. Developer settings (bottom-left)
  4. Personal access tokens
  5. fine-grained tokens
  6. "Generate new token"
  7. Write a Token name
  8. Pick an expiration date from the menu or a custom one
  9. Repository access> All repositories
  10. Open "Repository permissions" menu
  11. Look for the "Contents" row
  12. From the menu at right select "Access> Read and Write"
  13. "Generate token" (bottom-left)
Damian Green
  • 6,895
  • 2
  • 31
  • 43
Lucas
  • 458
  • 4
  • 6
11

Had the same error with a workflow that I've had already used in the past without any problem. Turns out that, in new repositories, workflows only have the Read permission. You need to enable the Read and write permissions in the repository settings.

Go to Settings > Actions > General > Workflow permissions and choose Read and write permissions.

Screenshot

Skyost
  • 1,429
  • 1
  • 16
  • 31
6

You need to get a write access from for the repo. Try asking your friend to give that. I solved it this way

Sardorek Aminjonov
  • 724
  • 2
  • 7
  • 16
3

You can resolve it by setting origin URL with your personal access token

  1. Open the terminal
git remote set-url origin https://{your-personal-token}@github.com/{organization}/{repo-name}.git
git push
Dave Lee
  • 316
  • 3
  • 9
3

For Fine-grained PAT After adding these access, I am able to pull and push into my repository.

github permissions

  • Thanks! I was afraid of being forced to read the entire (and always HUGE and useless) Microsoft documentation to understand such a simple thing. – Julio S. Aug 16 '23 at 14:32
2

I am trying to git clone a repo under another organization (owned by me too) through git bash by input username & fine-grained personal access tokens manually.

It took me a while to realize I have to select the resource owner to that organization when generating new fine-grained personal access tokens:

enter image description here

To have commit/push rights, also remember to grant Contents read-write permission:

enter image description here

Rohim Chou
  • 907
  • 10
  • 16
  • 1
    Note that this feature has to be enabled for the organization: https://docs.github.com/en/organizations/managing-programmatic-access-to-your-organization/setting-a-personal-access-token-policy-for-your-organization – Cyrill Aug 02 '23 at 12:20
1

I gave below permissions on the GitHub and it worked.

Permissions

Permissions

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
1
  1. Go to https://github.com/settings/tokens
  2. Click "Generate new token"
  3. Input token description e.g. "Sourcetree Mac Token", select "repo" checkbox, and click "Generate token"
  4. Copy the generated token
  5. Add your GitHub account to Sourcetree, but now rather than using OAuth, select Basic authentication
  6. Input your username
  7. Paste the generated token as password, Generate Key, and Save

Generate Key

JTL
  • 41
  • 3
1

One possibility is that the error message is misleading. I've seen this message appear when I forgot to add "Contents/read" access to the fine-grained personal access token. After adding the "read-only" permission, I was able to successfully clone the repository, even when the token has no write access.

Ted Brownlow
  • 1,103
  • 9
  • 15
0

Your friend as generate a Fine-grained personal access tokens and make sure you gives you permissions to the repo and user.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 09:37
  • I guess this means that the owner of the repository has to provide a fine-grained token to any collaborators but when using a classic token, that is not needed, it works just with `repo` scope selected. – rexxar Feb 06 '23 at 17:33
0

When you create (Personal access tokens) select under Permissions -> Repository ->permissions (select all read-write fields where possible) , do the same for (Account permissions ) then you will have all access and such an error should not occur. At least in my case, it helped, since all the answers in this article did not work for me.

MarkKush
  • 73
  • 8
0
  1. Create a fine-grained "personal access token" with correct code writing permissions: https://github.com/settings/tokens?type=beta

  2. Adjust the remote url:

git remote set-url origin https://{username}@github.com/{username}/{repo-name}.git
  1. Use the personal token as password
0

I Used Classic Token and pasted it in the password area in terminal. It Worked. If Your fine grain Tokens fails to work then try classic token and select the repo radio area like this one https://stackoverflow.com/a/71138077/17359554

0

For anyone who gets here in the future, after trying a few of the above solution and things still dont work. When creating a PAT:

  1. Repository access -> Only select repositories -> Give access to both main repo and submodule repo.
  2. Permissions -> Repository permissions -> Give Read-only access to Contents and Metadata.

expected permissions

A. Kali
  • 739
  • 6
  • 19
0

My GitHub Actions configuration contains instructions for fetching code with the custom bot's SSH key:

steps:
- uses: actions/checkout@v3
  with:
    ssh-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
    submodules: true

The same key will probably be used for pushing the code. Hence you have to give the write access to the bot.

0

I had such a problem when the repository owner gave me read-only rights