Force Git to show login prompt for re-authentication
— Git — 3 min read
My Github Personal Access Token expired recently. I went to my Git Bash terminal window expecting it to show me the login prompt where I can enter the new token. It showed me this prompt when I had first installed it. But this time when I did a git pull
or git fetch
it just kept giving me this error without showing me the login prompt.
remote: Invalid username or password.fatal: Authentication failed for 'https://github.com/foo/bar.git'
I decided to document the different approaches that have worked for me because this happens every once a month or 3 months and either I forget what I did last time or the approach that I had followed last time doesn't work anymore for whatever reason. So hopefully this post will help you if you're in the same situation as me.
Credentials Manager
Your first option would be to look into your OS's credentials manager if it has one. I use Windows and it has a Credentials Manager.
This is where Git stores credentials under the "Windows Credentials" tab in an entry named git:https://github.com
.
Delete existing git
Windows Credential
If you find this entry named git:https://github.com
, check the username and if it matches the Git account you want to change, either edit it and enter the new token as the password or just remove it.
Now go back to your terminal and do a git fetch
. If you had removed the entry from the credentials manager then Git should show you the login prompt where you can enter the new token. If you had updated it, then the command should proceed with doing what it does.
Manually add a git
Windows Credential
If the above approach doesn't work then you can also try adding a new Windows Credential manually.
Open the Windows Credentials Manager and click on a link that says "Add a generic credential".
Now for Internet or network address, enter git:https://github.com
. For username, enter your Github username and for password, enter the new Personal Access token you have created in Github.
Now open a new Git Bash or terminal window and try using git pull
or git fetch
commands and it should work.
Use git config
This is another approach you can try if the ones above don't work for you.
Step 1: Use this command and note down the resulting output value somewhere for use later.
git config --system credential.helper
Step 2: Now run the below command which will unset the existing credentials.
git config --system --unset credential.helper
Step 3: If you now run git fetch
, Git should show you the login prompt. Enter the new token and you should be good.
Step 4: We'll now restore the value we had copied earlier in Step 1.
git config --system credential.helper <copied value>
Hopefully one of these solutions should help return things back to normal!