Sub topic 6
Signing in with GitHub CLI
This chapter assumes you can clone, commit, and push, and that you know the difference between HTTPS and SSH remotes. It also assumes you have seen personal access tokens and credential helpers from earlier chapters.
What GitHub CLI adds
GitHub CLI (the gh command) is a separate program from Git. One of its jobs is to handle sign-in for you. Instead of creating a token in the web interface and pasting it into a prompt, you run one command. gh then walks you through the login and sets up the credential for Git.
Running the login
gh auth login
gh auth statusgh auth login asks a few questions:
- Which host to use: GitHub.com or a GitHub Enterprise Server.
- Which protocol Git should use: HTTPS or SSH.
- For SSH, whether to upload an existing public key or generate a new one. You can also set a passphrase.
- How to authenticate: through a web browser or by pasting a token.
The browser option shows a one-time code. You enter it on a GitHub page, approve the request, and gh receives an OAuth token. You can paste a personal access token instead if you work on a machine with no browser.
gh auth status then reports which account is signed in, which host it is for, which protocol is set, and which token scopes are in use.
What happens behind the scenes
Your protocol choice decides what gh configures.
| Protocol chosen | What `gh` sets up |
|---|---|
| HTTPS | Stores a token and registers itself as a Git credential helper for GitHub, so `git push` over HTTPS needs no prompt. |
| SSH | Uploads your public key to your GitHub account. Git then uses your SSH key, not a token. |
In both cases, gh also keeps its own token so that gh commands such as gh pr create can call the GitHub API. That token is separate from your SSH key.
Checking the result
After login, run git config --global --get-regexp credential if you chose HTTPS. You should see gh listed as a helper for https://github.com. Then try git push or git fetch on an HTTPS remote. It should work without asking for a password.
For SSH, run ssh -T [email protected], as covered in the chapter on testing the connection.
Common problems
- Scopes too narrow. If
ghcannot do something, such as read organization data, the token may lack a scope. Rungh auth refresh -s SCOPEto add one. - Organizations with SAML SSO. The token must be authorized for that organization before it works there.
- Wrong account.
gh auth statusshows the active account. If you see the wrong one, rungh auth logoutand sign in again.
Exercise
You ran gh auth login and chose HTTPS. A teammate says you should now see a prompt for a password on every git push. Why is that not expected, and which command shows which account and scopes gh is using?
Show answer
gh stored a token and registered itself as a Git credential helper for github.com, so Git gets the credential from gh without prompting. Run gh auth status to see the account, host, protocol, and token scopes.