Enabling https access to your GitHub repositories after enabling Two-Factor Authentication

If you have enabled two-factor authentication on your GitHub account (you should) you will no longer be able to push or pull code using https. I'll describe here the simple steps to set up a personal access token to allow you to use https for sharing your code on GitHub.

Create a GitHub Personal Access Token

Firstly log on to GitHub, access the 'Account Settings' area and locate the 'Applications' tab:

Applications Settings on Github

Select the 'Applications' tab and request to create a new personal token:

Personal Access Token on GitHub

Now provide a useful and recognizable name for your token and press the 'Create Token' button:

Personal Access Token on GitHub

You will now be provided with a very long unique string token that can be used to access GitHub. Make a copy of it as we will now use it to configure git on your local machine.

Configure you local git environment to use a Personal Access Token

Before trying to push any code to GitHub using the new Personal Access Token it is best to configure git not to continuously ask for your password. The simplest mechanism for doing this is to use a password cache.

Configuring a git credentials helper on linux (Debian)

> git config --global credential.helper 'cache --timeout=3600'

The command above configure the git credentials helper to ask for your password once and then store it for 60 minutes. Default behavior is to cache for 15 minutes but you can set this to whatever you like.

Configuring a git credentials helper on Mac

On Mac you can make use of the OSX Keychain to store your personal access token. To do so you need to have the osxkeychain credential helper installed. As I manage my git installs via Homebrew the osxkeychain credential helper is automatically available. If you need to install it you can follow the simple steps on GitHub here.

> git config --global credential.helper osxkeychain

Push or Pull your code to GitHub using https!

Now we have a Personal Access Key and git is configured correctly we can push/pull code to/from GitHub using https as before - remember your remote needs to be configured with the https url to do this:

> git remote -v
> origin    https://github.com/stephenhardy/aperi.git (fetch)
origin    https://github.com/stephenhardy/aperi.git (fetch)
> git push -u origin master
> Username: stephenhardy
> Password: yourlongpersonalaccesstokenhere

You can now successfully use https to communicate with GitHub again after configuring Two Factor Authentication!