Two-factor authentication in GitHub is pretty simple to set up, though the exact steps are layed out in maybe 3 different articles. I've consolidated them all together to this one simple guide.
Enable 2 factor authentication on the GitHub website (Settings → Security → Two Factor Authentication).
If you're using SSH keys to access your Git repos, you can skip this section. If you're using
https:// URL's in your Git repos, then read on!
On your computer, enable caching your GitHub HTTPS credentials. This allows you to store your 2FA token and not get asked for it everytime.
# MacOS: git config --global credential.helper osxkeychain
# Linux: git config --global credential.helper cache
This only applies if you use HTTP authentication for your Git repositories. If you use SSH keys, this shouldn't be necessary.
Generate a GitHub API key under "Personal Access Tokens". You'll use this for the Git command line. Leave the scopes unchanged.
Push a repo. You'll be asked for a password. Use the token for the password. You won't have to do this again if enabled credential caching.
$ git push
Username for 'https://github.com': rstacruz Password for 'https://[email protected]':
If everything worked, your
git push should work just fine.