Search code examples
gitmacosgithubterminalkeychain

Authentication to GitHub using personal access token on macOS?


After creating a personal access token, how do you unset any existing authentication and use the token in the macOS terminal?

I tried running osxkeychain command git config --global credential.helper osxkeychain but it doesn't change anything. I also tried unsetting the user password with git config --global --unset user.password, and I tried opening Keychain Access application and delete GitHub entries, but neither worked.


Solution

  • Step 1: Unset existing credentials

    Copy this into your terminal:

    git config --global --unset credential.helper
    git credential-osxkeychain erase
    host=github.com
    protocol=https
    

    and hit 'Enter' twice to delete existing GitHub credentials (username and password) from your computer.

    Recommended: if you were previously authenticating using a GitHub Personal Access Token (PAT) and want to update to a new one, invalidate any old one(s) by visiting https://github.com/settings/tokens and clicking 'Delete' or 'Revoke All' to ensure an old token can no longer be used.

    Step 2

    Generate a new access token: go to GitHub, click on your profile pic on the top right -> Settings -> Developer Settings -> Personal Access Tokens -> Tokens (classic) and create a new one (tip: if you're not sure what permissions to grant, just select full 'repos' permissions and nothing more - you can always create another token with more permissions later if required).

    Step 3

    Go into private repository and try to push/pull. GitHub will prompt you for your username (email or GitHub username) and password (the token you just generated).