How to remove cached credentials from Git?
Asked Answered
T

15

119

I ran:

$ git config credential.helper store

And then:

$ git push origin master

After pushing, I entered my credentials and they were saved.
I read that they are stored in plaintext, and so now I want to remove my credentials from being saved and entered by default.

How can I do this?

Tortola answered 29/5, 2017 at 16:24 Comment(1)
In order to keep control over the time that the credentials should be stored, I would recommend to edit git configuration for your OS user (~/.gitconfig OR $ git config -e --global) and add the following: [credential] helper = cache --timeout=7200Laconism
C
197

Run the following command in the terminal to remove your credentials stored in the cache

git config --global --unset credential.helper
Caldarium answered 16/10, 2017 at 7:16 Comment(8)
After running this command, my credentials are still stored in ~/.git-credentials. (Ubuntu 18.04, git version 2.17.1) So as in @torek's answer, you should delete this file as well.Aerograph
@Aerograph Try my answer below. I hope it would solve your problemNawab
For some reason, my credentials are still intact and I am still able to push/pull in all repositories. Any ideas why?Bathesda
this is NOT the answerArbitrary
The cleaning up of the ~/.git-credentials are very important. In windows-10, it is in c:/Users/<your_user_name>/.git-credentials.Kirkwall
I am getting Fatal: could not read Password for 'https://github.com': Input/output error ; afterwards I had to run git config --global credential.helper storeDoorjamb
This doesn't clear the store... it just makes it so you don't use the store at all. You have to enter your creds every single timeBugbear
echo 'url=https://git.example.com' | git credential reject is what you are looking forSieracki
E
34

Your credentials are stored in the file you (or the thing using git credential-store) specified when you (or it) ran the command, as described in the documentation. The default is $HOME/.git-credentials. You should be able to open this file in your editor and edit it, or simply remove it entirely.

Note that you may also want to change the credential helper so that these are not stored again. See the git credential-cache documentation as well, for instance.

Ethiop answered 29/5, 2017 at 18:21 Comment(1)
@IanKirkpatrick: there's also now a fancier way to reject one particular credential, but overall I think the whole credential system could stand some kind of regularization and overhaul. (But I'm not doing it myself either :-) )Ethiop
N
34

As he did not give any flag as global or local or system, it would take local by default, so the correct way is to move to the respective folder(repository) and type this command

git config --local --unset credential.helper

or

git config --unset credential.helper

Reference: https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config

Nawab answered 5/10, 2018 at 13:26 Comment(0)
O
20

for me unset command didn't work, instead I deleted the file:

rm ~/.git-credentials
Oliana answered 30/6, 2021 at 9:20 Comment(3)
That's what I needed too. Worked for me. thxRecommit
Same here. It was not working for me to do just: git config --global --unset credential.helper. Thanks!Downtrend
I also deleted a file name ~/git-credentials and it worked.Titled
V
19

If you would like the daemon to exit early, forgetting all cached credentials before their timeout, you can issue an exit action, run the following command.

git credential-cache exit

See documentation.

Victoir answered 20/6, 2018 at 15:29 Comment(1)
For anyone looking to re-enter their personal access token (PAT) because the old one expired: Use this approach. This will ask you for your PAT only once (at least until you type in a correct pwd), and then will save it for you. It does NOT turn off pwd caching, which is probably what you want.Hughes
G
14

for me all options didn't work, I just edited file "config" inside .git/ in my repository.

cd .git/

nano config

search for [remote "origin"] and edit above:

url=YOUR REPOSITORY URL (WITH OR WITHOUT user and pass like username:[email protected]/username/repository.git)

save and try push your repo

Gileadite answered 14/7, 2021 at 22:38 Comment(2)
Can also use git config --local remote.origin.url https://github.com/username/repository.gitTerisateriyaki
This worked for me since I was updating my git username.Monecious
E
10

If you're on windows 10, there is a credential manager in the OS to.

Go to Start menu, type "Credential Manager", then inside this manager, select "Windows Credentials", seek for "git:https//github.com", expand it, then you'll find "Delete" inside of it.

enter image description here

Epoch answered 17/12, 2021 at 19:25 Comment(0)
S
9

Universal and simple solution:

echo "url=https://github.com" | git credential reject

This cancels authorization on GitHub.

Shushubert answered 14/8, 2022 at 22:12 Comment(0)
V
8

For Linux users:

I ran into this question while trying to switch from password authentication to PAT (personal access token).

I used https to work with Github. Github suggests (at the time of writing this answer) that the switch can be done by simplying entering my PAT instead of my password. However, the problem I had, was that my password was cached, so I never had to enter it.

Source: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

So I had to look for my cached/stored password in order to remove it, so that I can then enter my PAT instead of my password when prompted.

I found the password in the following file:

/home/MyUsername/.gitconfig

I deleted the file, and simply pasted the PAT instead of my password the next time.

Github recommends using their Git Credential Manager Core (GCM Core) for storing credentials (source: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git). I installed this as well, and the PAT credentials are now stored in a different location:

/home/MyUsername/.gcm (it's a folder)

I wrote this answer in more details and perhaps a little offtopic as starting in 2 days (August 13th 2021), password authentication will no longer be possible, and I suppose some users might have difficulty in locating their cached password.

TL;DR look for:

/home/MyUsername/.gitconfig
Vaticinate answered 11/8, 2021 at 19:11 Comment(0)
P
7

None of the above worked for me. I am using a mac what i had to do was to remove my credential manually from keychain app

Steps

  • Ensure credential.helper is osxkeychain
$ git config --global credential.helper
osxkeychain
  • Open keychain app

  • Search for the credential (Eg Using username) by selecting (All Items) Key chain access

  • Press delete key Delete entry

  • Run the git command (push, pull etc). This should prompt your new password

Pasadis answered 28/7, 2021 at 7:43 Comment(1)
This is also discussed at docs.github.com/en/get-started/getting-started-with-git/… .Leptorrhine
S
7

If you want to remove just one cached credential then that can be done in any operating system by driving the Git credential helper API from the Git command line:

Let's say the Git repository is git.example.com and you connect using HTTPS. At the operating system command prompt enter the Git command:

git credential reject

You're now on the command line for the credential API's reject function. Type the following lines:

protocol=https
host=git.example.com

On the next line, close the commands with the "end of stdin" keystroke for your operating system. Typically Ctrl-D for Linux and Macos, and maybe Ctrl-Z for Windows.

The credential has now been removed from the credential store. Since we are using Git's Credential API for the removal, it doesn't matter which credential store implementing that API is being used for the backend.

Do not alter Git's configuration of user.email or credential.helper before using the above command. Run the command as the userid you use to connect with the remote Git repository, not as a super-user.

Sieracki answered 25/4, 2022 at 8:48 Comment(2)
I found I had to do this from the Ubuntu Terminal, doing it from the IDE terminal window did not workTwum
There's a one-liner for the above: echo 'url=https://git.example.com' | git credential rejectSieracki
E
4

None of the above solutions worked for me. I revoked the access by deletion of the token I had generated on the machine.

I logged into GitHub webpage went to: click picture > settings > Developer settings > Personal access tokens > delete the token for that particular machine

Eberta answered 16/11, 2018 at 2:36 Comment(0)
K
1

Oh goodness, here's another location - this one for KDE users. Spent a long time trying the ones already listed here...

Launch KDE Wallet Manager (also accessible from KDE Wallet settings in the system settings app), search for "github" and in the "ksshaskpass" section, remove the corresponding Github entry.

Kocher answered 27/12, 2022 at 23:16 Comment(0)
P
1

If you use github you can show your credential status with

gh auth status

login with credentials

gh auth login

and logout remove credentials with

gh auth logout

remove credential from cmd line

rm -rf ~/.config/gh
Pastor answered 7/9, 2023 at 11:5 Comment(0)
S
0

The below command helped me to resolve my issue:

echo "url=https://github.com" | git credential reject

Selfconceit answered 21/4 at 8:7 Comment(1)
There is already an answer that says that. Please refrain from duplicating content; use upvotes instead.Jarvey

© 2022 - 2024 — McMap. All rights reserved.