Github Logo

Set Up Git Credentials On Mac OS With Private Key (GitHub Access Token)

BlackSheep

--

As per the official GitHub blog post:

Beginning August 13, 2021, we will no longer accept account passwords when authenticating Git operations and will require the use of token-based authentication, such as a personal access token (for developers) or an OAuth or GitHub App installation token (for integrators) for all authenticated Git operations on GitHub.com.

The following article will tell you how to migrate to the new token based authentication (if you are currently using your password to authenticate) or setup git credentials on your Mac from scratch.

To get started , you might want to check out the steps to create your personal Github access token:

Once you have the key (PAT) , you can set up your credentials on your machine using the steps mentioned below:

1. Add your name and email to your global config file by running the commands mentioned below:

git config --global user.name 'Firstname Lastname'
git config --global user.email 'githubemail@domain.com'

Note: If you already have these credentials setup and you are currently using the deprecated ‘password’ method for authentication and choose to migrate to the token method , you can skip this step and make sure you delete any existing github credentials by going to the Keychain Access app and searching for ‘github’. Delete all entries that pop up under the aforementioned search result.

2. Set your global credential helper to ‘osxkeychain’

To check for your existing credential helper, you can run the following command:

git config --global credential.helper

Note: If you get keyword ‘store’ as result of running this command this means your credentials are being stored in your disk in an unencrypted file. Hence, it is not the safest way to store your credentials. Delete the ~/.git-credentials file on your system and run the following set of commands to change the credential helper.

On running this command , if you get anything other than osxkeychain (or nothing gets printed as a result) you can unset your existing credential helper and set it to osxkeychain by running the following set of commands:

git config --global --unset credential.helper
git config --global credential.helper 'osxkeychain'

Check the global config file using the command

git config --global --edit

And you’ll see the config file open up in edit mode on your terminal:

Global Config file (Credentials blurred out)

Verify your details and press esc then q to quit.

3. Add the token to your credential helper by running a git command that requires password to authenticate.

Run any git command (ex. git clone) and you will see the following prompt,

$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token (The token you generated from Github)

and make sure you fill in the correct GitHub username in the username field ,and your GitHub token in the ‘Password’ field respectively.

If you’ve correctly entered the aforementioned details you will get a new entry in your Keychain Access app under search result ‘github’. This is the encrypted entry of your github token and will be used for authentication in the future.

You can execute any git commands and will no longer be prompted to enter your GitHub credentials.

--

--

BlackSheep

An engineer sharing his thoughts after spending a few hours going down a rabbit hole.