error: The requested URL returned error: 403 Forbidden while accessing https://github.com/github-username/github-repository-name.git/info/refs
Let's see what are the possibilities for getting this error and how to solve it!
This error will occur when there is problem with Authentication or access permissions.
If you are trying to push your code into another person’s repository, make sure that you have the correct access or permission to do it.
If you are the owner and you still get the authentication error, this can happen for several reasons, they are,
Wrong username or password
access token got expired or revoked
Incorrectly configured SSH key
Changes in repository permissions
Wrong branch name
Two-factor authentication enabled on your account
Ensure that you are the owner and you have the necessary access to push the code.
Double check that you have the correct username and password or access token configures for your Git.
Ensure that you are using the correct and updated branch name
Ensure that access token did not get expired or revoked
Ensure that the access token is generated with all read and write permissions. If the access token got generated before and you dont remember or dont know whether the access token is having all the permissions or not, no worries! You can always generate a new token!
You are using correct username and password but still your access getting denied?
This might be because of cached credentials. Clearing this cached credentials is necessary when,You have changed your username and password but you have accidentally cached incorrect credentials and you want to remove the incorrect credentials.
‘git credential-cache exit’ is the command to clear the cache. This will clear all the cached credential data from stored memory.
Checking for correct or updated branch name
First of all, to list all branches of your remote repository, use the following command,
‘git branch -r ‘
This will list all the branches that are associated with your remote repository. Now you need to know in which branch you are currently in. For that use the following command,
‘git branch --show-current’
Now you can ensure that you are in correct branch. If not, To switch to another branch, use the following command,
git checkout [branchname]
If you want to create new branch using the terminal command, then,
git checkout -b [new-branch-name]
Sometimes the branch that you are trying to push must be protected and direct push are not allowed. Then you need to change the branch permissions.
Two-factor Authentication
If you have enabled 2FA, then you need to use personal access token. To set remote url with accesstoken, use the following command,
‘git remote set-url origin https://[access-token]@github.com/[username]/[reponame].git’
Generating new Access Token
Go to your GitHub account settings.
Navigate to "Developer settings" or "Developer settings > Personal access tokens".
Click on "Generate new token" or a similar option.
Give a token description and select the desired scopes or necessary permissions for the token. Ensure that all ‘write’ permissions are enabled. For pushing changes, the "repo" scope is typically required.
Click on "Generate token" or a similar button to create the token.
Copy the generated personal access token in some safe place with the date generated and the expiry date. So that you can refer it in the future. Always create token with expiry date for better security and controlled access.
Creating a new repository through terminal command
Go to terminal or command prompt.
Go to the directory where you want to create the repository.
Example: cd /user/eclipse-workspace/[ProjectName]
git init
git add .
git commit -m “comment”
— A new repository got created with the same name of the project. Now add remote origin by using following command.
Adding a remote repository
git remote add origin https://github.com/[username]/[ProjectName].git
‘git remote add’ is the command to add new remote.
‘origin’ is remote name
‘https://github.com/[username]/[ProjectName].git’ is remote URL.
Verify remote repository
To see whether the remote origin is added correctly, use the following command,
‘git remote -v’ - this command will display the remote origin with name and URL
If remote origin is already set, then you will see the following error
‘remote origin already exists’ - in this case you might need to reset the remote URL.For this use the following command,
Changing the remote repository url
‘git remote set-url origin http://github.com/[username]/[projectname].git’
‘git remote set-url’ is the command to change remote repository.
‘origin’ is the new remote name
‘http://github.com/[username]/[projectname].git’ is the new remote url.
Now verify the updated remote url by using ‘git remote -v’
After successfully adding the remote origin, now push the code by using following command.
git push -u origin master
Are you getting Forbidden error or Authentication error again?
We are getting this error because when we initially created or cloned the repository, there was a different access token. Later we generated new access token with all permission. So we need to include the new access token in the set-url method for authenticated actions like push or pull.
Authenticate the remote url with new access token
You need to use the following sert-url method with access token in the terminal or command prompt,
git remote set-url origin https://[username]:[your-token]@github.com/[username]/[repository].git
Summary:
Authentication failure will happen typically because of incorrect username, password or invalid accesstoken
Forbidden error will happen usually because of insufficient permissions to push to the repository or branch and the branch may have protection rules that blocks the push.
Hope this blog helps to you solve authentication/forbidden error and successfully able to push your code into git. Thanks for visiting my blog. Happy reading :)