Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ashish-ad/43f80e1093b54d3dc5fa34e2d534390d to your computer and use it in GitHub Desktop.

Select an option

Save ashish-ad/43f80e1093b54d3dc5fa34e2d534390d to your computer and use it in GitHub Desktop.
Adding & Updating GitHub Access Token on Mac

Using an Access Token for the first time

Alternative read -> Creating a personal access token

Create an Access Token

In your GitHub account, go to Settings -> Developer settings -> Personal access tokens and select Generate New Token. Make a note of the token somewhere safe since this is the only chance you get to see it.

Add the token to your OSX Key Chain

When you next interract with a private repository on the command line Github should challenge you for your credentials. When you are prompted to supply your Password for 'https://[email protected]': you enter your access token instead.

$ 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

Using the token on your Mac the first time should automatically add it to your OSX Key Chain so that you do not need to enter it every time you are interracting with the Github API. If you check your local git configuration you should see that there is a credential.helper key pointing to the OSX Key Chain.

$ git config -l

credential.helper=osxkeychain
[email protected]
user.name=Joe Bloggs

Updating an existing Access Token

Regenerate token on Github

If you need to regenerate or revoke an existing Access Token then log into your Github account and navigate to Settings -> Developer settings -> Personal access tokens and choose to either Generate new token, or replace your existing token either by choosing Delete and Generate new token, or viewing your current token and choosing Regenerate token.

Remove existing token from your Mac keychain

You can check if you have an existing token using the following command.

$ security find-internet-password -l github.com

Which should report something like this if it exists.

keychain: "/Users/jbloggs/Library/Keychains/login.keychain-db"
version: 512
class: "inet"
attributes:
    0x00000007 <blob>="github.com"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="joebloggs"
    "atyp"<blob>="dflt"
    "cdat"<timedate>=0x32303730303933373333343635395A00  "20200938235909Z\000"
    ...(truncated)

Or else if it does not exist then you should see something like this.

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
To erase an existing access token use:-
$ security delete-internet-password -l github.com

If you had a token previously stored then running the above should output something like the following.

keychain: "/Users/jblogs/Library/Keychains/login.keychain-db"
version: 512
class: "inet"
attributes:
    0x00000007 <blob>="github.com"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="joebloggs"
    "atyp"<blob>="dflt"
    "cdat"<timedate>=0x32303230303932383233343630395A00  "20200928234609Z\000"
    "crtr"<uint32>="aapl"
    ... (truncated)
password has been deleted.

Now, the next time you attempt a clone/pull/push etc on a private repo Github should prompt you for your Username/Password. Note that you enter your new token when prompted for Password.

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