Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active May 3, 2024 11:06
Show Gist options
  • Save ateucher/4634038875263d10fb4817e5ad3d332f to your computer and use it in GitHub Desktop.
Save ateucher/4634038875263d10fb4817e5ad3d332f 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 to authenticate with GitHub when you have 2-factor authentication set up. This authentication should be inherited by any GUI client you are using. These are intentionally brief instructions, with links to more detail in the appropriate places.

  1. Download and install the git command-line client (if required).

  2. Open the git bash window and introduce yourself to git (if required):

    git config --global user.name 'Firstname Lastname'
    git config --global user.email 'firstname.lastname@gov.bc.ca'
    
  3. Turn on the credential helper to cache your credentials (so you only need to do this once):

    a. Windows (more detailed instructions here):

    git config --global credential.helper wincred
    

    b. Mac (more detailed instructions here):

    git config --global credential.helper osxkeychain
    
  4. Set up a personal access token for accessing GitHub repositories - 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 a test repository in the bcgov organization.

  6. Clone the repository on the command line (terminal/git bash window):

    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. If your test repository isn't important, you can delete it from the bcgov GitHub account.

@olotintemitope
Copy link

Thanks

@cristianhoyos66-zz
Copy link

For linux to save the credentials (user and password whose value which will be the access token) you can use https://git-scm.com/docs/git-credential-cache with --timeout option defining the time you want it to be saved.

This a better option for me, howevere you can still use https://git-scm.com/docs/git-credential-store but it will save the data inside your disk.

@TheM0nk25
Copy link

What about VMs where you cant just c/p the token?

@paultech4u
Copy link

For Linux, you can:

  • Use credential.helper cache to cache for 15 minutes.
  • Use credential.helper store to store the credentials unencrypted on disk
  • Enter the access token every time

There are other helpers floating around such as gnome/libsecret

Thank you

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