154

I've followed the GitHub instructions for setting up my account, and I'm able to clone, but I'm unable to push remotely.

When I do a "git push" I get the 403 error. It has the correct URL. I tried a "git credential-osxkeychain get" to see what it was giving, and this prints out the wrong credentials.

I believe what happened is the first time it asked for the credentials, I thought it was another application asking, and it put the wrong ones in.

I just need to reset it so that it uses the correct keychain item for my GitHub account.

I've tried:

git credential-osxkeychain erase
git credential-osxkeychain set

The program never gives any prompts. Set will say "bad input" if I don't do the right thing. I tried putting in "password=password", etc., but then when I do a "get" I still get the old ones.

I can't figure out where these are being stored, as they are not in .gitconfigure. Further there are no recent keychain items that could be it. (I have several GitHub accounts in my keychain and it is not using any of them.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nirvana
  • 4,081
  • 4
  • 25
  • 29

10 Answers10

223

From Terminal: (You need to enter the following three lines)

 $ git credential-osxkeychain erase ⏎
 host=github.com  ⏎
 protocol=https   ⏎
 ⏎
 ⏎

NOTE: after you enter “protocol=https” above you need to press ~~RETURN~~ TWICE (Each '⏎' is equivalent to a 'press enter/return' )

Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
maz
  • 8,056
  • 4
  • 26
  • 25
  • 6
    For some reason I couldn't find any Git/GitHub credentials within the Keychain app, but this worked. Thanks! – Donald Mar 22 '16 at 15:35
  • 12
    Since this wasn’t clear to me: you have to enter all 3 lines above, and then press return again – i.e. lines 2/3 above are not output from the command, but input. – Frederik Mar 13 '18 at 11:45
  • This answer helped me. Thank you very much! – Ken Flake Oct 08 '19 at 00:39
  • The number of entering the enter key was the key. – DongBin Kim Jul 28 '21 at 07:51
  • 1
    can anyone please explain what this does? @maz – Ojasv singh Dec 27 '21 at 10:15
  • 2
    @Ojasvsingh it removes the github.com entry from your keychain. This means that the next time you attempt to connect it will be detected that there isn't an entry anymore. This means you will be prompted for your current credentials for the github.com – maz Dec 28 '21 at 20:24
  • Googled for hours and this helped :) So simple and straightforward!! – krupesh Anadkat Jan 25 '22 at 13:43
  • @Frederik I agree, the people who write git documentation at git think the users are mind readers. They are from planet "git" where everybody speaks gittish and should just assume. – Brian Wiley Feb 28 '23 at 03:26
  • Why is this `git` command so abstruse. `git credential-osxkeychain --help` doesn't seem to give any documentation. UPDATE [someone else noticed this](https://stackoverflow.com/q/51294589/5783745), and yep, it's more or less undocumented. Kinda wild since it has an unusual usage (pressing enter *twice*?!). – stevec Apr 04 '23 at 00:32
95

I'm not sure how to erase through the command line, but it's fairly easily to do it through the Keychain Access app. Just go to Applications -> Utilties -> Keychain Access, then enter "github.com". You can either delete the invalid item or update the password from with the app.

benzado
  • 82,288
  • 22
  • 110
  • 138
36

Try this in your command line.

git config --local credential.helper ""

It works for me every time when I have multiple GitHub accounts in OSX keychain

invinciblemuffi
  • 908
  • 1
  • 11
  • 22
  • 1
    This may not be the accepted answer basing on the question's title but it's definitely useful in the context that the questioner was trying to achieve. If your password has changed and git "remembers" the old one, simply run this command and try again. Thanks invinciblemuffi. – tuan.dinh May 19 '21 at 02:19
  • 1
    It is so useful for different user login. Very thank you ! – cyan-kinesin Mar 01 '22 at 15:23
  • Glad that a simple solution helps the community around :) – invinciblemuffi Mar 03 '22 at 05:15
  • 1
    this is the only answer that has worked for me on mac – user2848810 May 01 '22 at 21:47
  • 5
    Once the password is reset, for mac, running `git config --local credential.helper "osxkeychain"` re-enables the credential helper so git does not keep asking for the password going forward – Josiah Jul 19 '22 at 18:11
34

The solution turned out to be this:

The command git credential-osxkeychain was using the first GitHub account entry in my keychain. This one was not the one that had access to the projects in question.

I resolved the problem by touching the account in Keychain Access so that its date changed (I think I just changed the comment) and now that it became the most recent GitHub account it became the first one returned to credential-osxkeychain, and thus everything worked.

A better form of support for multiple GitHub accounts would be nice, but it is likely that most people only have one primary account and don't run into this problem.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
nirvana
  • 4,081
  • 4
  • 25
  • 29
  • 2
    I'm not sure how this works for multiple users on the same host, but you might check out ssh and using a `~/.ssh/config` file instead of using OSX Keychain. This is used in cases where you may have multiple ssh keys for different hosts(and thinking accounts as well). http://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/ – jusopi Mar 03 '15 at 13:51
10

git-credential-osxkeychain stores passwords in the Apple Keychain, as noted above.

By default, gitcredentials only considers the domain name. If you want Git to consider the full path (e.g. if you have multiple GitHub accounts), set the useHttpPath variable to true, as described at http://git-scm.com/docs/gitcredentials.html. Note that changing this setting will ask your credentials again for each URL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MacFreek
  • 3,207
  • 2
  • 31
  • 41
10

On Mac, use the command git credential-osxkeychain erase.

OR remove manually from keychain from ApplicationsUtilitiesKeychain Access. Then remove the github.com keychain. Then use push; it will ask for the keychain access; then deny.

It will ask for the new username and password, add it then pushes a file for that.

After git push I found this error. Then I use the upper case- issue:

remote: Permission to user1/file.git denied to user2(previously exist user ). fatal: unable to access 'https://github.com/xxxxxxxxxxxx/': The requested URL returned error: 403

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit kumar
  • 457
  • 6
  • 14
6

You can delete existing credentials and add new ones via "Keychain Access"

Image

Ref. https://docs.github.com/en/github/getting-started-with-github/updating-credentials-from-the-macos-keychain

Satheez
  • 570
  • 7
  • 14
  • This is the same comment and link as the one above by gorjanz. Furthermore, changing the credentials from the keychain isn't always possible, e.g. if you have more than one entry in keychain and don't know which one is the right one. In this case see my comment under gorjanz's post. – MDickten Mar 08 '22 at 17:52
5

GitHub help page for this issue: https://help.github.com/articles/updating-credentials-from-the-osx-keychain/

gorjanz
  • 1,954
  • 2
  • 18
  • 13
  • That link only tells you how to update your credentials from the app. If you don't want to do that (I couldn't because I had several github entries in the keychain and couldn't tell which was the right one) you can use the accepted answer [here](https://stackoverflow.com/questions/68824590/update-command-line-git-credential-helper-for-personal-access-tokens-pat-on-gi): You can invalidate your credentials via `$ echo url=https://account@github.com | git credential reject` (replace `account` with your account). Next time you push, git will prompt you for your password and store it. – MDickten Mar 08 '22 at 17:46
4

Try running /Applications/Utilities/Keychain Access.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
echristopherson
  • 6,974
  • 2
  • 21
  • 31
2

@bk2204 answer only worked for me from terminal:

$ echo url=https://github.com | git credential reject
KindDragon
  • 6,558
  • 4
  • 47
  • 75
  • True. It is [documented with Git 2.42 (Q3 2023)](https://stackoverflow.com/a/76592430/6309) – VonC Jun 30 '23 at 22:03