Log in to GitHub with the GitHub CLI
In order to push commits to GitHub you need to have authentication setup. As an alternative to ssh keys, you can use the GitHub CLI (gh) to log in to GitHub through your web browser. gh will then handle authentication for git whenever you push or pull over HTTPS, so you don’t need to create or manage ssh keys. The steps below will walk you through how to install gh, log in, and use it with git.
Before doing step 1, check if you already have gh installed. Open a shell and run:
gh --versionIf a version number is returned, skip step 1. Otherwise, if you get “command not found”, do step 1.
- Install
ghby following the instructions for your operating system on the GitHub CLI installation page. For example:- Mac (using Homebrew):
brew install gh - Windows (using
winget):winget install --id GitHub.cli - Linux: see the Linux installation instructions for your distribution.
gh --versionagain to verify that it worked. - Mac (using Homebrew):
| Note: you need a GitHub account to do the following steps. If you do not have a GitHub account, sign up for one by going to https://github.com and following the onscreen instructions. Once you have an account you can proceed. |
- Now let’s log in to GitHub. In your terminal run:
gh auth login --hostname github.com --git-protocol https --webThis tells gh to log in to github.com through your web browser and to use HTTPS (instead of ssh) for git operations. If you are asked Authenticate Git with your GitHub credentials?, type Y. You will then be shown a one-time code, something like:
! First copy your one-time code: XXXX-XXXX
Press Enter to open https://github.com/login/device in your browser...Copy the code, press Enter, then paste the code into the page that opens in your browser and follow the onscreen instructions to authorize the GitHub CLI. When you’re done, your terminal should say that you are logged in as USERNAME, where USERNAME is your username.
You can also just run gh auth login and answer the prompts yourself. If you do, choose GitHub.com as the host, HTTPS as the protocol, Y to authenticate Git with your GitHub credentials, and to log in with a web browser. The exact prompts may differ slightly depending on your version of gh.
- To test whether you were successful, in your terminal run
gh auth statusYou should get something like
github.com
✓ Logged in to github.com account USERNAME (keyring)
- Active account: true
- Git operations protocol: https
where USERNAME is your username.
- Step 2 should have also configured
gitto useghfor authentication. If you said no toAuthenticate Git with your GitHub credentials?, orgitlater asks you for a username and password when you push, run:
gh auth setup-gitYou’re now ready to use GitHub!
Using git without ssh
Because gh authenticates git over HTTPS, you need to use the HTTPS URL of a repository instead of the ssh URL. In the exercises, whenever you are told to copy the SSH URL from the green <> Code button on GitHub, select the “HTTPS” tab and copy that URL instead. It will look something like https://github.com/USERNAME/REPO_NAME.git (rather than git@github.com:USERNAME/REPO_NAME.git). For example:
git clone https://github.com/USERNAME/REPO_NAME.gitIf you already cloned a repository using its ssh URL, you can switch it to HTTPS by running the following inside the repository:
git remote set-url origin https://github.com/USERNAME/REPO_NAME.gitFor more information, see the GitHub CLI manual and GitHub’s documentation on caching your GitHub credentials in Git.