Authentication to GitHub using personal access token on macOS?
Asked Answered
I

4

35

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.

Inutility answered 22/5, 2021 at 5:23 Comment(0)
I
67

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).

Inutility answered 22/5, 2021 at 5:28 Comment(3)
I am running old High Sierra and it hangs at git credential-osxkeychain erase.Ocotillo
@Ocotillo I just read this and I think you need to copy all 4 lines in the first code section of my answer, then hit enter twice and it should work :)Inutility
Related: https://mcmap.net/q/22955/-why-is-there-no-documentation-anywhere-for-git-credential-osxkeychain/5783745Inutility
R
21

The below command worked for me on mac as mentioned in https://gist.github.com/jonjack/bf295d4170edeb00e96fb158f9b1ba3c.

security delete-internet-password -l github.com

After running this command in terminal, run a git clone command and it will ask you the username and password. On password please provide your access token which you created from the github web portal.

Ronnironnica answered 14/8, 2021 at 13:5 Comment(0)
D
14

TLDR; your token will be your new password. Storing it in the mac keychain makes it a hassle that you don't have to track. Here's the steps on how to update your password with the token.

I see that you've already created a token, which if reading the deprecation notice you might think that's all you need to do. Here's the instructions on creating a token for anyone that hasn't yet: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#using-a-token-on-the-command-line

But as you've mentioned, the next step is to replace using a password with instead using the token.

@stevec shared correctly how to do this on the terminal, but you may also open the keychain app and do it there.

  1. Use the mac search for "keychain" and open Keychain Access.app
  2. Search for GitHub. You may see two options: an application password and an internet password
  3. Open the application password. You may check the box for "Show password and likely see that this still uses your password rather than the token.

Replace that password with your token. That worked for me.

Discernment answered 22/5, 2021 at 13:51 Comment(1)
Thank you. Mine got deauthenticated somehow and did not want to re-gen everything. This solved it quicklyRelentless
N
1

Just as an aside... if you're moving from a classic password to a personal token from github and the repo already exists in your local environment, check the remote.origin.url which could continue to cause problems if the previous password is in the remote url. I followed all the above steps but until I reset the remote url, the auth problems persisted

e.g. git remote set-url origin "https://@github.com/<username/.git"

Newcomer answered 5/1 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.