How to Fix "Permission to x.git denied to user" Error in Git When Using Multiple Local Accounts?

Why Does This Happen?

If you have set up multiple git accounts on your local machine, and are getting the following error when running the git push command:

$ git push
ERROR: Permission to git_username/repo.git denied to <username>.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Then it is likely that the git remote URL (as specified in .git/config file in that repository's folder) does not match the Host nickname you specified in the ~/.ssh/config file.

For example, let's suppose you have the following two GitHub accounts setup locally in your ~/.ssh/config on your macOS:

Host work.github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-work
   IdentitiesOnly yes

Host personal.github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-personal
   IdentitiesOnly yes

Based on the configuration above, the .git/config file in your repositories should either have "personal.github.com" or "work.github.com" specified as the "url" under "[remote "origin"]" in .git/config. For example, the url property under [remote "origin"] might look like the following:

[remote "origin"]
    url = git@github.com:designcise/my-repo.git

This does not work, because git cannot find any exact matches for "Host" (i.e. there is no Host configured as "github.com" in your ~/.ssh/config file).

How to Fix the Issue?

To fix this issue you can do any of the following:

After doing any of the above, if you run git push, it should work correctly (given you have specified the correct Host and username for your repository).

Adding Entry for Default Host:

You can update your ~/.ssh/config file and create/update an entry matching [remote "origin"] url. For example, if the url is "git@github.com:designcise/my-repo.git", then you can create a Host entry for "github.com", like so:

Host github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-work
   IdentitiesOnly yes

Host personal.github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-personal
   IdentitiesOnly yes

Setting Remote URL Using git remote set-url Command:

You can update the remote url (specified in the .git/config file) via the command line, for example, by using the following command:

git remote set-url origin git@CUSTOM_HOST:GIT_USERNAME/REPO_NAME.git

For instance, if you want to use a Host nickname "work.github.com" (as set in ~/.ssh/config) and your git username is "designcise, then you can set the remote url like so:

git remote set-url origin git@work.github.com:designcise/my-repo.git

Updating Remote URL in .git/config File:

You can manually edit the .git/config file, and make sure the "CUSTOM_HOST" and "GITHUB_USERNAME" are set to the correct values:

[remote "origin"]
    url = git@CUSTOM_HOST:GIT_USERNAME/REPO_NAME.git

For example, if you want to use a Host nickname "work.github.com" (as set in ~/.ssh/config) and your git username is "designcise, then you can update the [remote "origin"] url, like so:

[remote "origin"]
    url = git@work.github.com:designcise/my-repo.git

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.