Skip to content

Instantly share code, notes, and snippets.

Star You must be signed in to star a gist
Save jonjack/bf295d4170edeb00e96fb158f9b1ba3c to your computer and use it in GitHub Desktop.
Adding & Updating GitHub Access Token on Mac

Using an Access Token for the first time

Follow the instructions on Github to Create an Access Token in Github

Configure Git to use the osxkeychain

By default, git credentials are not cached so you need to tell Git if you want to avoid having to provide them each time Github requires you to authenticate. On Mac, Git comes with an “osxkeychain” mode, which caches credentials in the secure keychain that’s attached to your system account.

You can tell Git you want to store credentials in the osxkeychain by running the following:-

git config --global credential.helper osxkeychain

Add your access token to the osxkeychain

Now issue a command to interract with Github which requires authentication, eg. git clone or git pull. When you are prompted to supply your Password for 'https://username@github.com': you enter your access token instead. Your token should now get cached in the osxkeychain automatically.

$ git clone https://github.com/username/repo.git

Cloning into 'repo'...
Username for 'https://github.com': your_github_username
Password for 'https://username@github.com': your_access_token

Updating an existing Access Token

Regenerate token on Github

If your existing token has expired, or been revoked, or you are on a new machine and do not have access to the existing token then you can regerate a new one in the Github console Settings -> Developer settings -> Personal access tokens.

Remove an existing token from your Mac keychain

You can remove an existing password or token stored in the osxkeychain using the following command.

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

You should now be prompted for your Github credentials when attempting a git pull/clone/push etc and your token should automatically get stored in the osxkeychain. If subsequent calls to Github repeatedly prompt you for your credentials then likely the credential.helper is not set - see next section.


Troubleshooting

$ git pull

remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user/repo.git/'

If authentication fails and you are not prompted to enter your credentials, like in the above example, or you are repeatedly challenged by Github for your credentials, then check if you have the credential.helper set.

git config --global credential.helper

If not, or it is set to something other than osxkeychain, then update/set it.

git config --global --unset credential.helper
git config --global credential.helper osxkeychain

Then try again and you should now be prompted for your credentials.

$ git clone https://github.com/username/repo.git

Cloning into 'repo'...
Username for 'https://github.com': your_github_username
Password for 'https://username@github.com': your_access_token

Ensure you provide your access token rather than password when prompted.

@SimonGolms
Copy link

After typing git credential-osxkeychain erase the display does not hang/load, instead it expects another input, see: https://stackoverflow.com/a/28007050

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

@jondhill333
Copy link

Yep, the above works, it just appears like its done nothing. However just go to push to Github again and you will see it asks for authorisation and it should be all working.

@frisbiesa
Copy link

I deleted the token from the keychain but I'm still not being prompted for my new api password. Super frustrating.

@startakovsky
Copy link

Same. This string of comments didn't help / weren't intuitive to me. Going to manually manipulate git config.

@benslack19
Copy link

benslack19 commented May 19, 2021

This was much more frustrating than I expected as well but got it to eventually work. I'm on MacOS Catalina. Here were my steps:

  • I deleted within the Keychain Access app based on this GitHub page.
  • It wouldn't prompt me for a password after a git push or a git clone of a private repo.
  • I then tried git credential-osxkeychain erase but it hangs.
  • I tried git config --global credential.helper osxkeychain, which was taken from [here].(https://gist.github.com/nepsilon/0fd0c779f76d7172f12477ba9d71bb66).
  • I then re-started my computer.
  • It worked after the re-boot.

@Archaeo-Programmer
Copy link

My solution ended up being much simpler than the suggestions above (at least for macOS Mojave version 10.14.6). After following the instructions above to generate/re-generate (I had to regenerate a token due to the new authentication token formats), I tried the following:

  • I did git pull with the hopes of being prompted to update my password. But it just said that the password failed.
  • I tried looking into keychain access, but nothing came up when searching for git / GitHub
  • I then tried git credential-osxkeychain erase, but mine also hangs.
  • I then simply quit the terminal, then opened it back up and went to my git directory, did git pull again and then it prompted me for my user name and password.

@WANGJIEKE
Copy link

WANGJIEKE commented Jun 29, 2021

My solution consists this step

After typing git credential-osxkeychain erase the display does not hang/load, instead it expects another input, see: https://stackoverflow.com/a/28007050

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

and then a git config --global credential.helper osxkeychain (though this step may be unnecessary), then finally a git pull on an existed repository.

@terryjray
Copy link

Using the 'git credential-osxkeychain erase' method above with the host and protocol options did work for repos that I could delete and reclone. The big issue for me was that I had a local commit that I needed to push when github decided to finally stop allowing passwords. None of the steps above allowed me to push my local commit to remote. The only thing I could do was manually edit the .git/config in my repo and manually replace my old password with the token. That did the trick but I think having to do this is ugly and hacky and I wish it wasn't necessary :(

@lucidlive
Copy link

Fresh Big Sur 11.1 install. I want to remove a Github token from the keychain.

git credential-osxkeychain erase hangs.

I can see a github.com entry in the Keychain Access UI, but I cannot remove it.
If I filter for that entry with search box, right-click and Delete "github.com" does nothing.
If I clear the filter and find the entry in the list, Delete option is not available in the context menu.

I ended up using a command line tool to remove the token from the keychain:

security delete-internet-password -l github.com

(You may want to use security find-internet-password --help to find the entry first.)

Thanks, same issue and this worked for me

@binDongKim
Copy link

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

This worked for me. The tricky / essential part is the "enter" key. You need to keep entering the enter key in each line.
After this, when you try any git command such as git push, the new configuration setting will be prompted.

@tresf
Copy link

tresf commented Jul 28, 2021

The lower paragraph uses the words "prompt you for your token". I would recommend it's changed to match the paragraph above which specifically states "prompt you for your Password" to match. Also, the paragraph above might have a missing _ after the word password.

- Now, the next time you attempt a `clone/pull/push` etc on a private repo, the Github API should prompt you for your new token on the command line and entering the new token should also add it to your local Key Chain which you can check as below.
+ Now, the next time you attempt a `clone/pull/push` etc on a private repo, the Github API should prompt you for prompt you for your _Password_ on the command line and entering the new access token should also add it to your local Key Chain which you can check as below.

@jessewaites
Copy link

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

This worked for me. The tricky / essential part is the "enter" key. You need to keep entering the enter key in each line.
After this, when you try any git command such as git push, the new configuration setting will be prompted.

For anyone searching, this is the correct solution.

@jonjack
Copy link
Author

jonjack commented Sep 9, 2021

tresf

The lower paragraph uses the words "prompt you for your token". I would recommend it's changed to match the paragraph above which specifically states "prompt you for your Password" to match. Also, the paragraph above might have a missing _ after the word password.

- Now, the next time you attempt a `clone/pull/push` etc on a private repo, the Github API should prompt you for your new token on the command line and entering the new token should also add it to your local Key Chain which you can check as below.
+ Now, the next time you attempt a `clone/pull/push` etc on a private repo, the Github API should prompt you for prompt you for your _Password_ on the command line and entering the new access token should also add it to your local Key Chain which you can check as below.

Thanks, I updated.

@tuyenphk
Copy link

tuyenphk commented Oct 6, 2021

Hey, it is the first time I access the MacOS so based on the new restriction of GitHub on August 2021, the internet password for github.com on keychain access does not show up here. Thus, how can I enter username and password for git on my terminal? Thanks!

@christophergraber
Copy link

I opened my VScode on Mac and it prompted "Keychain Access wants to user your confidential information stored in 'github.com' in your keychain. I looked in my Keychain and did see a github.com entry and was able to copy the password. But when I entered it in the popup box it never worked. I deleted the Keychain entry manually. Tried making a new personal access key, restarting, doing git clone etc, but was never getting prompted for my new password/PAC.

Copy link

ghost commented Oct 20, 2021

Thank you it solved my problem

@cadetCoder
Copy link

After typing git credential-osxkeychain erase the display does not hang/load, instead it expects another input, see: https://stackoverflow.com/a/28007050

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

This works for me too! Big help! Thanks

@Mehuge
Copy link

Mehuge commented Nov 30, 2021

All I did was edit .git/config and change pass = <old-token> to pass = <new-token>. Perhaps my git is not configured to use osxkeychain?

Visual Studio Code did its own thing and asked github.com to authorise access using some fancy link.

I have to say, its all a bit of a mess though, it should be as simple as telling git to update token.

@sebmellen
Copy link

For anyone potentially struggling with this, try opening the Keychain application in OSX and changing the Personal Access Token value there. This worked very well for me. See attached screenshot.

Photo of using Mac OS Keychain to edit values for Github Personal Access Token

@fijisoo
Copy link

fijisoo commented Jul 9, 2022

For anyone potentially struggling with this, try opening the Keychain application in OSX and changing the Personal Access Token value there. This worked very well for me. See attached screenshot.

Photo of using Mac OS Keychain to edit values for Github Personal Access Token

this is a solution for me! thanks!

@wuqinqiang
Copy link

Keychain

so cool

@niams-bdmds
Copy link

cool. this worked

@szfkamil
Copy link

Thanks. The $ commands did not work for me, I just read and re-read the steps and analysed the errors, and it worked. Took like 10 minutes; thanks for the comprehensive guide!

Might take some trial and error especially for people new with using Terminal, but it'll work!

@braedonwatkins
Copy link

braedonwatkins commented Nov 13, 2023

Fresh Big Sur 11.1 install. I want to remove a Github token from the keychain.

git credential-osxkeychain erase hangs.

I can see a github.com entry in the Keychain Access UI, but I cannot remove it. If I filter for that entry with search box, right-click and Delete "github.com" does nothing. If I clear the filter and find the entry in the list, Delete option is not available in the context menu.

I ended up using a command line tool to remove the token from the keychain:

security delete-internet-password -l github.com

(You may want to use security find-internet-password --help to find the entry first.)

This worked for me. What a hunt to find this fix.

For reference git credential-osxkeychain erase didn't hang for me, I suspect some people think it hangs when it just needs user input, but the CLI tool was the one to fix it for whatever reason.

@jeremykoerber
Copy link

None of this worked for me. Using gh auth refresh worked though.


! First copy your one-time code: XXXX-XXXX
Press Enter to open github.com in your browser...
✓ Authentication complete.```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment