Increase your productivity with the Github CLI

falcao_g

Falcão

Posted on February 26, 2024

Increase your productivity with the Github CLI

If you use Github on a daily basis and still don't know about the Github CLI, I recommend you to keep reading this article. Today we are going to learn how to increase our productivity and quality of life when working with issues, pull requests, actions, and more!

Table of contents

Instalation

Before we start, we need to install the CLI to be able to use it, as there are many operating systems in this beautiful world, I will leave here a link to the installation guide from Github itself.

With the tool installed, we can check the version with the command:

gh --version
Enter fullscreen mode Exit fullscreen mode

Authenticating

After installing, we need to authenticate to be able to enjoy the tool, this is done with the command:

gh auth login
Enter fullscreen mode Exit fullscreen mode

To verify that you are indeed logged in, run this command:

gh auth status
Enter fullscreen mode Exit fullscreen mode

A message saying that you are logged to Github in your account should appear.

Repositories

With the Github CLI, we can clone, fork, list, view a repository, and much more.

Let's say we would like to see all our repositories, we can use the gh repo list command.

A table showing all my repositories

This is the command output, a very friendly little table that shows the names, descriptions, and some other information of all my repositories, note that it says it is showing 30 of 31, this is because this command only shows repositories in which I am the owner, the 31st is a repository that I am only a contributor, so it is not shown.

We can also create a repository, with gh repo create, this command helps a lot to simplify the process of creating and setting up a completely new repository:

A questionary asking for the name, owner, description, visibility and some other helpful stuff

Cloning is also simplified, we can do it the way we are already used to with git, using the repository URL:

Successfully cloning using the url

Or even better, doing gh repo clone OWNER/REPO:

Successfully cloning using the OWNER/REPO syntax

With gh repo fork, the syntax is the same.

With this command, we can also archive, delete, rename, and sync repositories and more!

Issues

We work a lot with different issues daily, the CLI also makes our life easier in this regard, removing the need to leave the terminal to open/view/list/comment etc.

As expected, to create an issue, we use gh issue create:

Questionary asking the issue's title, body and if you want to submit, continue in browser or cancel

Similarly, if we want to see the issues created, we use gh issue list:

A list of issues showing labels, titles and when it was created

If we want to see only issues with a specific label, we can use the --label flag, for example, to see only issues with the "bug" label, we can do gh issue list --label=bug (in my case the only issue is already a bug, but it works!).

To get more information about a specific issue (for example, the real issue instead of just the title and label), we use the gh issue view <issue_number> command:

Terminal showing the issue title, body and status

In this case, the information is easily understood by the terminal itself, however, sometimes we have larger media or texts and seeing the issue through github would be better, for that we can use the --web flag.

This way you see the issue in the browser

We can comment on open issues using the gh issue comment <issue_number> command:

Questionary asking the comment content and if you want to post it or not

Additionally, it is also possible to close, reopen, delete, pin issues and much more.

Pull requests

By far, what I like most about using the CLI is how much it simplifies working with pull requests, for example, let's say we have a new branch with changes already committed and now we want to create a PR for the repository, instead of going through the hassle of uploading a new branch and creating it through the site, we can simply do gh pr create.

Questionary asking the pr's title body and if you want to submit
If we need to add some kind of media, we can simply continue in the browser, but all the information will already be there.

Just like in issues, we can also use gh pr list and gh pr view, to list all pull requests and see more details about a specific one respectively.

Imagine that someone has just opened a PR in the repository, and now? how to test the code? It is possible to check out locally with gh pr checkout <pr_number>, so your git will create a new branch in your local repository, with all the changes presented in the pull request:

It creates a new branch with the code presented in the pr

It is also possible to comment on a pr with gh pr comment <pr_number>:

Same as commenting on an issue

Now let's say you do have checks configured for new pull requests, such as linters or automated tests, you can check the status of a specific pr with gh pr checks <pr_number>:

Showing one check of aa linter that passed and took 2 min and 19 seconds

In addition, we can also review, mark a pull request as ready for review, close, reopen, merge, and more, all through the terminal.

Aliases

Like any terminal tool, it's always good to create aliases for commands you use frequently, we can do this using the gh alias set command. For example, let's create an alias called "bugs", which will show us only the issues with the "bug" label, to achieve this just type in the terminal:

gh alias set bugs 'issue list --label=bug'
Enter fullscreen mode Exit fullscreen mode

Now we can at any time type "gh bugs" in the terminal and see all related issues :)

Additionally, we can delete an existing alias with gh alias delete <name> or view all created aliases with gh alias list.

Actions

If you use Github Actions, another power of the CLI is the ability to control and view relevant information directly in the terminal, with gh run and gh workflow, the difference is that workflow refers to the files and run the runs that the actions have already had.

To list all recent runs, we use gh run list:

list of runs showing their titles, status, time taken and workflow

We can also see details of a run or jobs using gh run view:

Showing more info of a run, like its individual jobs

The same goes for workflow, however, with this subcommand we can also use enable and disable, to enable and disable a specific workflow file.

If your workflow receives the workflow_dispatch event, it is possible to activate it manually with the command:

gh workflow run
Enter fullscreen mode Exit fullscreen mode

Learning more

We only scratched the surface of what the Github CLI is capable of, some of the other features that we can manipulate with this tool include: releases, projects, organizations, labels, gists, Github Actions variables, and more!

To better understand how it can help you in your day-to-day life as a developer, I recommend exploring the existing commands with gh help:

This command shows all commands and a small description of what they do, as well as your aliases and what command they represent

Note that our alias commands also appear here, which is a nice addition.

If our doubt is about a specific command or subcommand, we can always use the syntax gh help <command> <subcommand>, for example:

A long description of what the command pr create does
Flags for that command
Examples and where to learn more

We got a super detailed description of exactly what this command does, the available flags, the aliases, examples, and more information!

Extensions

Every good tool needs to have extensions made by the community, and here it is no different, we have several official and community-made extensions to further boost your CLI. To start, use the command:

gh extensions browse
Enter fullscreen mode Exit fullscreen mode

A list of all extensions with a preview of their readme in the right

Here we can see all the extensions, see their repositories, and install them if we want, if you want to find a specific extension, you can use the search subcommand:

Here i am searching for a star extension

Installed an extension and now decided you don't want it anymore? No problem, just use the gh extensions list command to see all installed extensions and gh extensions remove to remove one of them:

Successfully removing the dash extension

Conclusion

I hope you enjoyed this article! I personally use this tool daily and it has helped me a lot, especially in open source projects, I hope it can help you too.

If you want to read more about it, here is the official manual.

If you have any questions, suggestions, or feedback, feel free to leave a comment, I will be happy to answer, thank you for reading!

💖 💪 🙅 🚩
falcao_g
Falcão

Posted on February 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related