See GitHub's Two factor authentication articles on help.github.com.
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).
Install Google Authenticator, or an equivalent of such. Works for iOS and Android. In Authenticator, tap Add, then Scan Barcode. Take a picture of the QR code on your computer. (More info on apps)
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.
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.
If your git repos still use SSH ([email protected]:user/repo.git
), change them to use HTTPS (https://github.com/user/repo.git
). (More info on remote URLs)
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.
Get your recovery codes (Settings → Security → Two Factor Authentication → Recovery Codes) and put them somewhere safe. This will allow you to log onto your account when, say, your phone isn't charged. (More info on recovery codes)
If everything worked, your git push
should work just fine.