GitHub CLI in combination with fzf

Tadej Golobic
Geek Culture
Published in
4 min readSep 21, 2020

So, lets talk about GitHub CLI. Last week, on 2020–09–17, GitHub released first stable version of its CLI tool. This tool can help us creating and listing pull requests, we can also create new issues, list issues, etc. Lets check in practice, what this tool can do.

First, we need to install it. On macOS it is simple as this:

brew install gh

Next thing that you need to do is authentication. You can authenticate with two different methods.

  • Credentials authentication
  • GitHub token authentication (env variable must be set)

First, lets choose type, and lets run the following command

gh auth login

You will be prompt to choose into what account do you want to login, and how would you like to authenticate. I will leave this part up to you to decide on how would you like to authenticate.

Now that we are through the authentication, lets move to more fun stuff :)

Lets create our first issue. To create an issue, we must run the following command

gh issue create

I think that this command is self explanatory :) You will be prompt to enter title, body, you will then choose if you want to add some metadata like assignee etc. At the end, submit it.

Created issue with gh

Go ahead, and create some other issues. After you are done, you can list your issue with this command:

gh issue list

and if you want to see specific issue, you just need to run following command

gh issue view #numberOfIssue

We can do all this things with pull requests. Just instead of issue keyword, type pr keyword, and all will work as a charm.

But lets face it, we do not want to see list of issues, especially if there is hundred of issues. We want to search through this issues or pull requests. Here comes fzf tool. I am in love with this tool. If you do not know about this tool, check it out. But this is quick introduction to fzf tool:

fzf is a general-purpose command-line fuzzy finder. It’s an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Let’s try and combine gh and fzf. We can search through issues like this

gh issue list | fzf

This will give you something like this

gh issue list in combination with fzf

So now, you just go and start typing. Beautiful part of all this is that you can search by status (OPEN), by title, or by date. Go ahead and try.

Search by title
Search by date

Isn’t this great? It is, but there is no big benefit for us. We need to see the issue in terminal, we want to open this issue etc. Well, we can use preview option of fzf like this

gh issue list | fzf --preview "gh issue view {-1}"

With this command, we are able to preview an issue. We can still search through issues, but we will also be able to see more information regarding this issue

gh issue with fzf preview

But if you want, you can also open this issue in browser like this:

gh issue list | fzf --preview "gh issue view {+1}" | awk '{print $1}' | xargs gh issue view --web

Since fzf is returning issue (line that you see when in fzf), we need to extract issue number (first column) and pass it to gh issue view command. To open issue in browser, we must append web switch to command.

As I already said before, you can do also this with pull requests. To check all diffs in pull requests, you can use this command:

gh pr list | fzf --preview "gh pr diff --color=always {+1}"

By now, it should be clear to you what this line does. Nothing special. It just previews the changes in pull request. I haven’t tested this on large pull requests…yet.

I believe that this tool will help me save some time creating PR, creating releases, etc. But there are still some things that I want to have. I want to use (n)vim instead of nano to write issue body. I would also like to have some option, to see what files/commits were changed between two releases, etc. But this are some small things that does not outweighs all good things that are in this first stable release.

--

--

Tadej Golobic
Geek Culture

Dad, Husband, Software Engineer and wannabe Foodie.