GitHub has officially released their GitHub CLI beta which is written in Golang! This is truly a game-changer for anyone using GitHub actions for professional or personal purposes. It has never been easier to interact with GitHub at a programmatic level. GitHub CLI allows you to do anything from clone repositories to create branches to interact with PRs.

GitHub CLI Tutorial

In this tutorial we will take a programmatic look first-hand on how to interact with the GitHub CLI. We will customize our workflow with an existing repository. I will be using my PwshDocker module that I created and can be found here.

Github HUB

Installation will depend on the Operating System you are running. All installation methods can be found here. In my case, I will be installing on Windows using the chocolatey Windows package manager.

Installing Github HUB

If you're using Windows, you will run choco install hub at the terminal. If you are using OS X, you will run brew install hub at the terminal.

To confirm that GitHub CLI is installated, simply type hub on your terminal and hit enter.

You have now successfully installed GitHub CLI

Cloning a repository with GitHub CLI

You're now ready to break into using the CLI. Time to clone a repository!

Choose any repository you'd like. It could be one of yours or a repository that's public in GitHub. I'm going to work with my PwshDocker repository.

On the command like, change directory to a directory that you would like to clone to. In my case it's C:\Users\mike\Documents\GITREPOS

Now it's time to clone the repository. Run the following GitHub CLI command:

hub clone your_repository

See the screenshot below of what my output looked like:

You have successfully cloned your repository using GitHub CLI!

Check the status of a GitHub respository

Checking the status of a GitHub repository is crucial to getting a solid understanding of where your repository currently stands.

We can do this using the ci-status to show the status of checks for a commit.

At the terminal type the following command:

hub ci-status

If this is the first time you're interacting with hub, you may be prompted to type in your GitHub credentials.

github.com username: your_username:
github.com password for your username (never stored):>/code>


You can now see any specific status to a commit on your repository!

Github GH

The GH command line is also part of the hub GitHub CLI command line and is what developers will mostly be interacting with. GH can be installed the same way we saw in the previous section installing GitHub HUB

Installing GitHub GH

GH can be installed with both chocolatey and brew.

brew install gh
choco install gh

In my case because I'm on Windows, I will be using chocolatey.

To confirm gh is installed, you may have to close out of your terminal and re-open.

Creating a new branch in your repository

To create a PR, you will need to have a new branch so it can be merged into master. On your terminal, type the following command to create a new branch:

hub checkout -b Develop

You will now have a develop branch

Next you can create any type of commit you want. Create a text file that says "hello" in it, for example. The type of commit isn't important for this. We just need to have some find of commit so it can get merged into master.

Once you make some kind of commit, push it to your Develop branch.

Creating a PR with GitHub GH

Now that gh is installed, it's time to create a PR. Change directory into a GitHub repository of your choosing.

At the terminal, run the following to create a new PR

If this is your first time interacting with gh, you may be prompted to open a web browser and authenticate.

Once the green Authorize github button is clicked, you will be prompted to type in your Github password.

It is quite possible that you will also receive an email from GitHub stating that a first-party Github OAuth application has been used. This is fine. It was just you authenticating to GitHub.

Head back over to the terminal and hit enter for the authentication completion.

You will now be prompted to type in information for your PR.

The first piece of information is the title. Type in whatever you'd like. As shown in the screenshot below, I named mine testPR.

For the next part you're going to see Body and if you type anything, a notepad will pop up. This is where you'll type in your body/description for your PR. As shown in the screenshots below, I'm going to put test PR:

After you type in your body/description, save by hitting the "X" on the top-right corner on your notepad and click save. This will automatically initiate the terminal to move on.

In your terminal (and in the screenshot below) you'll see a few options:

  • Preview in browser
  • Submit
  • Cancel

Move the arrows on your keyboard up and down and hit enter to make a decision. I like to live dangerously, so I'm going to use the Submit option and hit enter.

A PR has been successfully committed.

Checking the status of a PR using GitHub GH

In the previous section we created a PR. Now it's time to check the status of that PR.

At your terminal, run the following command to check the status of your PR:

gh pr status

Per the screenshot below, you can now successfully see any status updates regarding your PR.

Summary

In this post you took a first-hand approach on working with the GitHub CLI. You saw not only how to install GitHub CLI on any operating system but how to interact with the CLI.