How to update an expired Personal Access Token (PAT) in Git?
A quick guide to update an expired Personal Access Token in a non-destructive way using Git CLI.
Updated 1 min read
- #Git
- #Fix
Following my other “quick” guides (also known as personal notes 🤣) like How to solve the ReferenceError Document is not defined issue?, I decided to write this short one, just to provide a direct answer to anyone looking for this on Google.
1. The situation
I was working in a GitLab project and… I was not able to fetch / pull the latest changes:
remote: HTTP Basic: Access denied. If a password was provided for Git authentication, the password was incorrect or you're required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped. See <https://gitlab.com/help/topics/git/troubleshooting_git.md#error-on-git-fetch-http-basic-access-denied>
fatal: Authentication failed for '<https://gitlab.com/*********/************.git/>'
Basically, I had my PAT (Personal Access Token) expired. So… time to renew it.
2. Get the new token
I was using GitLab but wherever you are hosting the repo, you will have a way to generate a new personal access token. Just do it.
In case you are using GitLab, here is a direct link.
3. Updating Git
Once you have the token, it is time to open a terminal in the root directory of the project with the expired token.
The first command to run is:
git config --get remote.origin.url
Why? Because you will need to “build” the new origin URL by updating its token.
Probably your command return will look like this:
https://<YOUR_GIT_USER>:<YOUR_EXPIRED_PAT_TOKEN>@gitlab.com/********/************.git
Building the new origin URL
Now just replace the expired PAT token string with the new one you generated, so you have a new URL like this:
https://<YOUR_GIT_USER>:<YOUR_NEW_PAT_TOKEN>@gitlab.com/****
Updating Git
Now you can run the following command (replacing it with your new URL):
git config remote.origin.url <YOUR_NEW_ORIGIN_URL>
4. That’s it!
If you now fetch or pull, it should work. That was easy, wasn’t it?
git fetch && git pull