Skip to main content

Fix 'Write access to repository not granted' in Git/GitHub

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The fatal: unable to access error with the message "Write access to repository not granted" typically means you don't have the necessary permissions to push changes to the GitHub repository. It's a clear signal from the server that your authentication credentials, while valid, don't grant you the required write access.

Here's how to diagnose and solve this issue:

1. Check Your GitHub Repository Permissions

First, verify that your GitHub account has write access to the repository you're trying to push to.

  • Navigate to the repository on GitHub.
  • Go to Settings > Collaborators and teams (or Manage access for organization repositories).
  • Search for your username and confirm that your role is at least Write (or Maintain). If you only have Read access, you will be unable to push.

If you don't have the correct permissions, you'll need to contact the repository owner or an administrator to request them.


2. Verify Your Authentication Method

The error can sometimes be a red herring caused by a problem with your authentication. Git uses different methods to authenticate with GitHub, and an incorrect setup can lead to this error.

If you are using the HTTPS URL (https://github.com/<user>/<repo>.git), Git uses a personal access token (PAT) or the GitHub CLI to authenticate. A common issue is an expired, revoked, or incorrectly scoped token.

Solution:

  • Go to Settings > Developer settings > Personal access tokens in your GitHub account.
  • Ensure you have a token with at least the repo scope enabled.
  • If your token is expired or missing, generate a new one (1).
  • Use the new token to authenticate the next time you push. Git Credential Manager (GCM) is recommended for storing and managing these tokens securely.

Using SSH

If you are using the SSH URL (git@github.com:<user>/<repo>.git), the error suggests a problem with your SSH key. While less common for a write access error, it's worth checking.

Solution:

  • Make sure your SSH key is added to your GitHub account (2).
  • Open your terminal and run ssh -T git@github.com. You should get a message confirming successful authentication. If not, your SSH key isn't set up correctly.

3. Check the Repository's Push Rules

The repository owner may have applied branch protection rules that prevent you from pushing directly to a specific branch (like main).

Solution:

  • Check for branch protection rules in the repository settings (Settings > Branches > Add rule).
  • If a rule is in place, you may need to submit a pull request instead of pushing directly.
  • The error message will usually be more specific in this case, mentioning "protected branch," but it's worth checking if the above solutions fail.

Summary

ProblemDiagnosisSolution
No write accessYou're not a collaborator or team member with write permissions.Ask the repository owner or an admin for Write or Maintain access.
Expired/invalid tokenYou're using HTTPS and your PAT has expired, been revoked, or has insufficient scopes.Generate a new PAT with repo scope and store it securely with a credential manager.
SSH key issueYou're using SSH and your key isn't correctly added to your GitHub account.Run ssh -T git@github.com to confirm. Add your SSH key to GitHub if it's missing.
Branch protectionThe branch you're pushing to is protected, preventing direct pushes.Push to a new branch and open a pull request instead.

By systematically checking these points, you can quickly identify and resolve the root cause of the "Write access to repository not granted" error.

Sources

  1. Managing your personal access tokens
  2. Adding a new SSH key to your GitHub account