Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tsai-jimmy/aa4434879f03e6684e24b80958d63237 to your computer and use it in GitHub Desktop.

Select an option

Save tsai-jimmy/aa4434879f03e6684e24b80958d63237 to your computer and use it in GitHub Desktop.
Setup git on the CLI to use 2FA with GitHub

These are instructions for setting up git on your Windows machine to authenticate with GitHub when you have 2-factor authentication set up. This authentication should be inherited by any GUI client you are using.

  1. Download and install the git command-line client

  2. Introduce yourself to git:

    git config --global user.name 'Firstname Lastname'
    git config --global user.email '[email protected]'
    
  3. Turn on the credential helper to cache your credentials (so you only need to do this once):

    git config --global credential.helper wincred
    
  4. Set up a personal access token for github - I recommend giving it gist, repo, and user scope. Make sure you copy the token now as you won't be able to later

  5. Create test repository in the bcgov org.

  6. Clone the repository on the command line:

    git clone https://github.com/bcgov/[my-test-repo]
    
  7. Make any change you want in your local repository. Eg., make or edit your README.md file.

  8. Add, commit, and push your changes:

    git add README.md
    git commit -m "Edit README"
    git push -u origin master
    

    At this point you'll be asked for your username and password. Enter your username, then in the password prompt paste your Personal Access Token you created in step 3. (Note: in the windows git bash shell, paste with Shift+Insert or right-click -> paste)

  9. Now push AGAIN.

    git push
    

    You should NOT be asked for your username and password, instead you should see Everything up-to-date.

    Rejoice and close the shell.

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