GitHub CLI is Now Available: Here’s Why You Should Be Excited

As of January 2020, GitHub reports having over 40 million users and more than 100 million repositories(including at least 28 million public repositories), making it the largest host of source code in the world. (Wikipedia)

As with any successful modern tool, Github is extremely extensible. It offers a great variety of ways to customize it, extend it, and integrate it with other tools.

But still, you might wonder — Why do we need a GitHub CLI while we already have a rich set of CLI commands with the Git CLI itself?

The main reason here is that the GitHub platform provides way more features than those available in Git version control. Plus, you can argue, we should not have to think of Git and Github as separate tools since the latter uses the former and extends its functionality.

GitHub already had a CLI called Hub maintained by one of GitHub’s employees. But they later decided to create this official project from the ground up rather than extending the Hub project, as discussed in the CLI vs. Hub article.

GitHub CLI brings the GitHub to the terminal. GitHub team releases version 1.0 in September 2020, and CLI helps developers create automated scripts for their workflows. With GitHub CLI you can;

  • Perform the entire GitHub workflow from the terminal, starting from creating an issue up to its release.
  • Has the ability to call GitHub API and script any actions.
  • Set a custom alias for any command.
  • Connect to GitHub Enterprise Server (GHES) in addition to GitHub.com.

1. Clone a Repo

You can perform repository related tasks using GitHub CLI, such as creating, cloning, and fork repositories. If you need any help, use the GitHub CLI --help command parameter, as shown below.

gh repo --help

To clone a repository from GitHub, you can use the following command.

gh repo clone owner/repo
Figure 1 — Clone Repository

2. Create Issue

Using GitHub CLI, you can create Issues. This feature gives you the ability to automate the Issue creation with pre-defined templates, labels and even assign developers as required. GitHub CLI also supports interactive inputs so that you can create Issues with guidance without opening the browser.

gh issue create
Figure 3 — Create a GitHub Issue

Also, you can list issues with the command shown below.

gh issue list
Figure 2 — List GitHub Issues

3. Create and Merge Pull Request

GitHub CLI provides a rich set of features around Pull Requests. Let’s look at a few essential tasks you can do with Pull Requests.

gh pr create
Figure 4 — Create Pull Request

After you create the Pull Request, you can approve and review it from GitHub CLI. This feature becomes handy for scenarios where you need to analyze code and change PR’s status accordingly. It also provides the ability to approve the Pull Request if it meets specific criteria using CI/CD. Besides, you can merge the changes into the branch, as shown below.

gh pr merge
Figure 5 — Merge Pull Request

In addition to these CLI commands, the list of options available with Pull Request is quite long. I’ll leave them for you to discover. You can follow the official documentation for more information.

GitHub CLI comes with powerful features to extend its core capabilities. You can extend the CLI using;

  1. Create Custom commands (or shortcuts) using gh alias set
  2. Make Custom API queries using gh api
  3. Use environment variables to set environment-specific configurations for your scripts.

Create Custom Commands

One of the most appealing features of GitHub CLI is the ability to create custom CLI commands using gh alias set. You can define any frequently used commands with parameters as custom commands. Let’s look at an example I’ve created to find the GitHub issued assigned to me.

In the example, I have mapped the shorthandgh myissues to gh alias set myissues 'issue list -A kasunsjc' as shown below.

Figure 6 — GitHub CLI allies

Make Custom API Queries

This is another powerful feature with GitHub CLI. You can define custom API queries using gh api <endpoint> [flags] that triggers an HTTP request to GitHub API and returns the response.

There are two ways to define the API query using the CLI. One approach is to use an API endpoint path with placeholders to fill the parameters.

gh api repos/:owner/:repo/issues/22

The following example shows how to query a specific issue from GitHub CLI.

Figure 7— Query GitHub API

The other approach is to use GraphQL when defining queries, where we have more flexibility on the return values.

gh api graphql -F owner=':owner' -F name=':repo' -f query='
query($name: String!, $owner: String!) {
repository(owner: $owner, name: $name) {
releases(last: 5) {
nodes { tagName }
}
}
}
'

For more details, refer to the documentation ingh_api.

I personally use these features to integrate (even further) Bit, the cloud component hub, with Github. Bit offers automated PRs to all relevant repositories, whenever a shared component (used by these repositories) is modified.

By making use of both custom commands and queries I can manage these PRs and get a quick overview of which project uses which shared components or which still uses a dated one.

Components shared on Bit.dev

Environment Variables

GitHub CLI has useful environment variables that will make your life easier when working with CLI. A few notable environment variables are EDITOR GH_REPO GITHUB_TOKEN. You can find more information about this by referring to environment variables.

If you look at the power of extensions, it won’t stop here. We can also improve the CLI script’s maintainability by creating command shortcuts for frequently used ones and encouraging reuse. It will also make the CLI scripts cleaner and easy to understand. I’m pretty sure you will love it!

Image by Free-Photos from Pixabay

After the release of GitHub CLI, it gained positive feedback from the developer community and brings lots of promises for the future. As an open-source tool, CLI keeps on continually improving and adding new features regularly.

New EcoSystem

When we look into the future, GitHub CLI has the potential of building a robust ecosystem well beyond the list of commands and extensions it offers. In the future, we could find reusable code snippets for different operations with GitHub.

For example, suppose your development team uses GitHub for source control and to track developer tasks using GitHub Issues. In the future, we should be able to find code snippets to bootstrap labels, create issues using templates, automate workflows &, etc. using GitHub CLI.

Road Map

As you go through the list of features and enhancements from the community, you can also identify several key features in the road map, as shown below.

In this article, I’ve provided an introduction to GitHub CLI and its feature set to speed up your adoption. While trying it out, I felt that it’s relatively easy to learn for anyone who knows the GitHub features.

Besides, it’s more convenient for DevOps operations compared to their REST API. However, for anyone who still uses the GitHub web portal, I feel it’s worth giving it a try and find out how it affects your productivity.

Finally, if you have any questions or clarifications, please comment below.

Learn More

Author: admin

Leave a Reply

Your email address will not be published.