Getting started with GitHub CLI

kasuken

Emanuele Bartolesi

Posted on October 28, 2022

Getting started with GitHub CLI

I have to admit it: I am not a big fan of command line tools.
But I do an exception for the GitHub CLI or GitHub Command Line Interface.

I like GitHub CLI because you can follow the entire GitHub workflow from the command line with zero context switching between the console and the website.

Let's see how to get started with it.

Installation

As the 90% of the GitHub products, you can install the CLI in every OS out there.
You can navigate to the home of the tool https://cli.github.com/ and download the right version for your OS.
If you are using Windows 11, you can use Winget with the following command:

winget install --id GitHub.cli

You can also update the CLI with the command:

winget upgrade --id GitHub.cli

You can find the installation instructions from the command line in the official repository located here: https://github.com/cli/cli

Login into your account

One the tool is installed, you can launch the following command in a command prompt:

gh auth login

Choose GitHub or GitHub Enterprise and in the next step, I recommend to you to use the "Login with a browser option".
It's easier and quicker.

Now you are ready to start to interact with GitHub from the command prompt.

Working with a repository

This command maybe is the most common used command with GitHub CLI: gh repo.
First of all, all commands start with the word "gh".

To interact with the repositories, use the command "gh repo" followed by one of the commands below:

archive: Archive a repository
clone: Clone a repository locally
create: Create a new repository
delete: Delete a repository
deploy-key: Manage deploy keys in a repository
edit: Edit repository settings
fork: Create a fork of a repository
list: List repositories owned by user or organization
rename: Rename a repository
sync: Sync a repository
view: View a repository

For instance, if you want to clone a repo, use the command:

gh repo clone charmbracelet/soft-serve

The first parameter is the owner' account name and the second one is the name of the repo.
This is the output after the last command.

Cloning into 'soft-serve'...
remote: Enumerating objects: 3481, done.
remote: Counting objects: 100% (1317/1317), done.
remote: Compressing objects: 100% (510/510), done.
remote: Total 3481 (delta 926), reused 963 (delta 766), pack-reused 2164
Receiving objects: 100% (3481/3481), 704.11 KiB | 5.10 MiB/s, done.
Resolving deltas: 100% (1895/1895), done.
Enter fullscreen mode Exit fullscreen mode

Working with the issues

One of the most important features of GitHub are the Issues.
And of course, you can manage the issues from the GitHub CLI with the following command after switching in a GitHub repo cloned on your machine.

gh issue list

In this case you obtain the list of the issues for that repo:

gh issue list

There are a lot of actions for the Issues as well:

close: Close issue
comment: Add a comment to an issue
create: Create a new issue
delete: Delete issue
edit: Edit an issue
list: List issues in a repository
reopen: Reopen issue
status: Show status of relevant issues
transfer: Transfer issue to another repository
view: View an issue

Working with Pull Requests

To close the GitHub workflow from the CLI, now we talk about Pull Requests.
With the the command gh pr create you can create a new Pull Request in the repository, directly from the CLI.
You have two follow some steps but it's very quick.
Then, the CLI opens a browser to insert the description and create the pull request from the web.

From the command prompts now you can merge the pull request.
Launch the following command and take notes about the Id:

gh pr list

gh pr list

With the number of the PR you can checkout the code with the command:

gh pr checkout [PR ID]

When you are ready to merge the PR, launch the command:

gh pr merge

The following list is the list of the actions for the Pull Requests:

checkout: Check out a pull request in git
checks: Show CI status for a single pull request
close: Close a pull request
comment: Add a comment to a pull request
create: Create a pull request
diff: View changes in a pull request
edit: Edit a pull request
list: List pull requests in a repository
merge: Merge a pull request
ready: Mark a pull request as ready for review
reopen: Reopen a pull request
review: Add a review to a pull request
status: Show status of relevant pull requests
view: View a pull request

Conclusion

The previous steps and commands are the very basic of GitHub CLI but with this information, you can start to work with it.
I recommend to you to read the documentation and take a look to the inline help using the command gh followed by the name of GitHub feature.


Thanks for reading this post, I hope you found it interesting!

Feel free to follow me to get notified when new articles are out šŸ™‚

šŸ’– šŸ’Ŗ šŸ™… šŸš©
kasuken
Emanuele Bartolesi

Posted on October 28, 2022

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

Sign up to receive the latest update from our blog.

Related