Skip to content

Instantly share code, notes, and snippets.

@jonjack
Last active February 19, 2025 20:43
Show Gist options
  • Save jonjack/bf295d4170edeb00e96fb158f9b1ba3c to your computer and use it in GitHub Desktop.
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://[email protected]': 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://[email protected]': 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://[email protected]': your_access_token

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

@ifm-pgarner
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.)

Thank you!

This was the only thing that worked for me

Issue seems related to having multiple github accounts - I had to delete the credential for the other one before it would prompt me to re-authenticate the one I wanted to use with the new token

@kmooventhan98
Copy link

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.

but this is for project based right? There is no .git file in my home directory @Mehuge

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