Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save trevorlinton/5ea1a1e4afb87377dff68b1e32520e24 to your computer and use it in GitHub Desktop.
Save trevorlinton/5ea1a1e4afb87377dff68b1e32520e24 to your computer and use it in GitHub Desktop.

Using Github with Two Factor Auth.

It's a fairly common complaint that after enabling github two factor authentication that command line utilities stop working. The underlying issue is command line utilities send your username and password with each request to github, using two factor authentication disables github from accepting just your username and password, so your command line utilities such as git appear to stop working. Github will still accept a personal access token instead of your password however.

Re-enabling your command line utilities (OSX)

You'll need to store your personal access token in your netrc as two factor authentication prevents your username/password from being used on the command line. To fix this follow these steps:

  1. Go to https://github.com/settings/tokens/new
  2. Put in the name "Command Line" for the Token Description
  3. For the scopes, enable all the top scopes (e.g., click the checkboxes: repo, admin:org, etc, etc...), then click Generate Token
  4. You'll need to copy the token it generates, it's in a green window with a clipboard icon next to it.
  5. Open a terminal window (Go to the Applications folder, Utilities folder, run Terminal application)
  6. touch ~/.netrc; open -a "TextEdit" ~/.netrc (or the CLI editor of your choice)
  7. Enter:
machine github.com
login [your_github_login_name]
password [the_token_copied_from_above]
machine api.github.com
login [your_github_login_name]
password [the_token_copied_from_above]

Make sure to replace [your_github_login_name] with your github login, NOT your email address. Make sure to replace [the_token_copied_from_above] with the token.

  1. Save these changes, and close the editor.
  2. Run chmod 600 ~/.netrc in the terminal
  3. To test this run curl -n https://api.github.com/user, you should see your user information in a JSON response. You can also test this by trying to clone out a private https repo, it should no longer ask for your password.

Re-enabling your command line utilities (Windows)

  1. Install git system wide, (see https://git-scm.com/download/win)
  2. Install Microsoft Github/Git Credential Manager (see https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/tag/v1.12.0)
  3. If you're using SourceTree or another software that uses github, make sure its set to use the system wide git and not its embedded one.
  4. Use things as normal, when prompted you'll be asked for your username and password; you may also be asked for your two factor auth token when logging in (it'll auto magically appear as a pop up when needed).
@kbberker
Copy link

Worked for me, thank you very much!

@arpo
Copy link

arpo commented Oct 8, 2020

Thank you 🙏🏽

@knpFletcher
Copy link

super helpful, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment