GitHub and GitLab Command Line Interfaces (CLIs)

Code hosting platforms like GitHub and GitLab provide command-line interfaces that make it possible to interact with their services from the terminal. These CLIs can also manage authentication to their respective services, ensuring that Git functionality in each editor works smoothly.

gh for GitHub

  1. Install the GitHub CLI:

Follow these directions to install the CLI for all Workbench users.

Download a binary from the releases page and unpack to a location on the PATH 1, such as ~/.local/bin:

$ wget -P /tmp https://github.com/cli/cli/releases/download/v2.14.4/gh_2.14.4_linux_amd64.tar.gz
$ tar xzvf /tmp/gh_2.14.4_linux_amd64.tar.gz -C ~/.local/ --strip-components=1 
  1. Using the terminal in your preferred editor, authenticate to GitHub by running:

    $ gh auth login
    • Select yes if you are prompted to re-authenticate; this will ensure that your authentication token has all the necessary scopes.
    • Select HTTPS as your preferred protocol.
    • Select yes if you are prompted to Authenticate Git with your GitHub credentials
    • If you select “Login with a web browser”, you will need to manually open https://github.com/login/device in another tab.

    Example gh auth login output

     ~
      gh auth login
     ? What account do you want to log into? GitHub.com
     ? You're already logged into github.com. Do you want to re-authenticate? Yes
     ? What is your preferred protocol for Git operations? HTTPS
     ? Authenticate Git with your GitHub credentials? Yes
     ? How would you like to authenticate GitHub CLI? Login with a web browser
    
     ! First copy your one-time code: 9D3B-A1FD
     Press Enter to open github.com in your browser... 
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: www-browser: not found
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: links2: not found
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: elinks: not found
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: links: not found
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: lynx: not found
     /usr/bin/xdg-open: 851: /usr/bin/xdg-open: w3m: not found
     xdg-open: no method available for opening 'https://github.com/login/device'
     ! Failed opening a web browser at https://github.com/login/device
     exit status 3
     Please try entering the URL in your browser manually
     ✓ Authentication complete.
     - gh config set -h github.com git_protocol https
     ✓ Configured git protocol
     ✓ Logged in as workbench_user
  2. Configure git to use GitHub CLI as a credential helper:

    $ gh auth setup-git
  3. Check your git config:

    $ git config --global --list
    credential.https://github.com.helper=!/usr/bin/gh auth git-credential
    credential.https://gist.github.com.helper=!/usr/bin/gh auth git-credential

The gh CLI should be configured as an HTTPS credential helper.

glab for GitLab

  1. Install the GitLab CLI

  2. Create a Personal Access Token. glab requires at least the api and write_repository scopes.

  3. Using the terminal in your preferred editor, authenticate to GitLab by running:

    $ glab auth login
Example glab auth login output
? What GitLab instance do you want to log into? gitlab.com
- Logging into gitlab.com
? You're already logged into gitlab.com as workbench_user. Do you want to re-authenticate? Yes

Tip: you can generate a Personal Access Token here https://gitlab.com/-/profile/personal_access_tokens
The minimum required scopes are 'api' and 'write_repository'.
? Paste your authentication token: **************************
? Choose default git protocol HTTPS
? Authenticate Git with your GitLab credentials? Yes
- glab config set -h gitlab.com git_protocol https
✓ Configured git protocol
- glab config set -h gitlab.com api_protocol https
✓ Configured API protocol
✓ Logged in as workbench_user
  1. Check your git config:
$ git config --global --list
credential.https://gitlab.com.helper=!/usr/bin/glab auth git-credential

The glab CLI should be configured as an HTTPS credential helper.

Back to top

Footnotes

  1. Type echo $PATH at the terminal to discover which directories are on the PATH.↩︎