DEV Community

loading...
Cover image for Getting Started With the Github CLI

Getting Started With the Github CLI

pratikgchaudhari profile image Pratik Chaudhari ・5 min read

GitHub, the popular Git Provider has created a command-line tool that allows its users to access GitHub without leaving their terminals!


In this post, I'll walk you guys through the process of setting up the GitHub CLI on your local machine.

So, let's start!

Step 1 - Install the GitHub CLI

Before you can start using the GitHub CLI, you must install it on your local machine.

Installing on Mac

If you're using macOS, you can install it using Homebrew, like so:

brew install gh
Enter fullscreen mode Exit fullscreen mode

Here's a gif to give you a better idea:

Installing the GitHub CLI. Image Credit: Pratik Chaudhari

Installing the GitHub CLI. Image Credit: Pratik Chaudhari

Installing on Linux

For Linux users, too, Homebrew should work just fine.

Although, if you're using a Linux distro that isn't officially supported by GitHub, you'll have to rely on the community-maintained versions of the CLI.

Instructions for installing the CLI on such distros can be found here.

Installing on Windows

If you're a Windows user, you can use Chocolatey to install the CLI, like so:

choco install gh
Enter fullscreen mode Exit fullscreen mode

Once installed, you can access it using the gh command, like so:

gh
Enter fullscreen mode Exit fullscreen mode

Here's what the above command will output:

Output after firing the 'gh' command. Image Credit: Pratik Chaudhari.

Output after firing the 'gh' command. Image Credit: Pratik Chaudhari.

Now that the CLI has been installed and is accessible via the Terminal, let's do something interesting using the CLI, like viewing a GitHub repository:

gh repo view pratikgchaudhari/vue-3-projects
Enter fullscreen mode Exit fullscreen mode

The above command is supposed to display all the info of the vue-3-projects repo created by its owner pratikgchaudhari, i.e., me.

But here's what we get instead:

Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.

Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.

The GitHub CLI requires you to authenticate yourself with your GitHub credentials before doing anything else with it!

And that brings us to our next step!

Step 2 - Authentication

To authenticate yourself, punch in the following command:

gh auth login
Enter fullscreen mode Exit fullscreen mode

Here's what you'll see:

Output of 'gh auth login' command. Image Credit: Pratik Chaudhari.

Output of 'gh auth login' command. Image Credit: Pratik Chaudhari.

The GitHub CLI is asking you whether you want to sign in using your account on GitHub.com, which is free for all users, or whether you want to use the GitHub Enterprise account (most probably owned by your employer or maybe just you).

Since I don't have an enterprise account, I'll proceed with my GitHub account.

Here's what I see after selecting the said option:

Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari.

Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari.

Since I don't have an authentication token (yet), I'll choose the Login with a web browser option.

Once I choose the said option, the CLI provides me with a one-time code:

Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari.

Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari.

I copy the one-time code provided by the CLI and press the Enter/Return key.

As expected, a browser window opens where I need to paste the one-time code that I just copied:

The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari.

The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari.

After I click the Continue button, I see the following screen:

The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari.

The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari.

Basically, I'm being prompted to authorize the GitHub CLI to access various resources from my GitHub account.

So, I'll grant it the access that it requires by clicking on the Authorize github button. Here's what I see next:

The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari.

The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari.

The above screen tells me that the authentication process is complete!

When I switch to my Terminal and press the Enter/Return key, the CLI asks me to choose between the following two protocols that it'll use while communicating with the GitHub server:

  1. HTTPS
  2. SSH

The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari.

The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari.

I'll choose the SSH protocol here, but there's no harm in selecting the HTTPS protocol either:

Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari.

Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari.

And it's done!

Let's execute the following command again and see what happens:

gh repo view pratikgchaudhari/vue-3-projects
Enter fullscreen mode Exit fullscreen mode

Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.

Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.

As you can see in the above screenshot, the title of the repository along with the contents of its README are displayed on the Terminal.

The GitHub CLI provides many more commands which I'll be covering in detail in a separate article in the future.

For now, this is it!

Discussion (0)

Forem Open with the Forem app