Follow the instructions on Github to Create an Access Token in Github
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
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
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
.
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.
$ 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.
I also found that the command 'git credential-osxkeychain erase' hung, even when using the longer form here: https://docs.github.com/en/free-pro-team@latest/github/using-git/updating-credentials-from-the-macos-keychain#deleting-your-credentials-via-the-command-line
Deleting from the KeyChain Access App worked fine - one note is that there will likely be multiple entries for GitHub.com and it is the "internet password" entry for github.com that you want to delete.
If you are worried about deleting a key you can't recover again by mistake, you can copy the existing key from keychain access by right clicking it also - with all the usual caveats that as soon as you save it someplace you need to be very careful to fully delete it afterwards if you don't want a copy of it.