How To Fix 'write access to repository not granted' (Git)

Written by: Donovan / Last updated: Aug 17, 2023

When working with Git repositories, especially on GitHub, you might come across the error message:

write access to repository not granted.

I’ll walk you through the common causes of this error and how to get rid of it using the command line and the GitHub GUI.

The ‘write access to repository’ error

Possible causes:

  1. Not having collaborator access: If the repository belongs to someone else or an organization, you might not have been given write access.
  2. Pushing to a protected branch: Some branches, like master or main, can be protected against direct pushes.
  3. SSH key issues: If you’re using SSH to connect to GitHub, your SSH key might either be missing or lack necessary permissions.

Solutions to try:

1. Checking and granting collaborator access (in the GitHub GUI):

  1. Login to your GitHub account and navigate to the repository in question.
  2. Click on the Settings tab.
  3. In the left sidebar, click on Manage Access.
  4. If you’re the owner, you can click on Invite a collaborator to add someone. If you’re not the owner and don’t see your username in the list, ask the owner to add you.

2. Handling protected branches (GitHub GUI also):

  1. Navigate to the repository and click on the Settings tab.
  2. In the left sidebar, click on Branches.
  3. Under the Branch protection rules section, click on the branch in question.
  4. Make sure the Allow direct pushes option is enabled or consider pushing to a different branch and creating a pull request.

3. Setting up SSH keys (command line):

If you haven’t set up your SSH keys or are unsure, follow these steps:

1. First, check if you already have SSH keys: bash ls -al ~/.ssh

2. If you don’t see id_rsa and id_rsa.pub, create a new SSH key: bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

3. Add your SSH key to the ssh-agent: bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

4. Copy your public key to the clipboard: - macOS: bash pbcopy < ~/.ssh/id_rsa.pub - Linux (you might need to install xclip): bash xclip -sel clip < ~/.ssh/id_rsa.pub - Windows (Git Bash): bash clip < ~/.ssh/id_rsa.pub

5. Go to GitHub and navigate to Settings > SSH and GPG keys > New SSH key. Paste your copied public key into the text field.

At this point, try your Git operation again.

If you still face issues, double-check repository settings, branch protections, and ensure you’re pushing to the correct branch.

View Archive