Top 10 Tips for using GitHub from the Command Line
Tags: cli, github, tutorial, git
Published: 2023-10-13 21:47:59

The [GitHub CLI](https://cli.github.com/) (_gh_ cli) is a command line tool that allows you to interact with GitHub without ever leaving your terminal. It's an open source tool that allows you to view and manage issues, pull requests, releases, and more. In this post, we'll cover 10 tips for using the GitHub CLI, but first, how do you install it?

## Installing the GitHub CLI :hammer_and_wrench:

I'm using MacOS, so I'll be using [Homebrew](https://brew.sh/) to install the GitHub CLI. If you're using Windows or Linux, you can find instructions for installing the GitHub CLI [here](https://github.com/cli/cli#installation).

```bash
brew install gh

```

Once you've installed the GitHub CLI, you'll need to authenticate with GitHub. You can do this by running the following command:

```bash
gh auth login

```

This will open a browser window and prompt you to login to GitHub. Once you've logged in, you'll be prompted to authenticate with GitHub. Once you've authenticated, you'll be able to use the GitHub CLI. :tada:

## CLI Syntax and Usage :memo:

The CLI follows a very specific syntax. It generally works as, "_gh_" "feature" "then what you want to do with the feature" then “any flags associated with the feature” - everything you want to do with the cli takes on this format.

For example, if you wanted to clone a repository using the _gh_ cli, it would be `gh repo clone <repo to be cloned>`

Let's take a look at some practical tips when using the GitHub CLI.

## 1. Create a new repository and clone it locally :tada:

You can create a new repository using the `gh repo create` command. This command will prompt you for the name of the repository, the description, and the visibility. You can also specify the name of the repository as an argument to the command.

You can also create a new repository and clone it locally at the same time by using the `--clone` flag. Here's what this will look like:

```bash
gh repo create your-new-repo --public --clone

```

<img src="https://github.com/LadyKerr/simple-next-app/assets/47188731/c2ba02cb-c753-4656-82de-da5f0928f8c9" alt="gh repo create command"/>

The repo was [created here](https://github.com/LadyKerr/gh-cli-example-repo) and all you need to do now is to push your changes and you're good.

## 2. Create issues :pencil:

We can create issues for this repository right from the command line with the _gh_ cli. This command will prompt you for the title and body of the issue. You can also specify the title and body as arguments to the command.

```bash
gh issue create -t "issue title" -b "issue body content" -a "assignee"

```

You can also edit, transfer, delete, close, and reopen issues with the _gh_ cli. You can find more information about this [here](https://cli.github.com/manual/gh_issue).

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/f7a9328c-5a60-4af0-a12d-54e4cfc32e01" alt="gh repo issue create"/>

## 3. Create pull requests :arrows_counterclockwise:

We can create pull requests by using the `gh pr create` command and follow the on-screen prompts. You can also specify the title and body as arguments to the command.

```bash
gh pr create -t "pull request title" -b "pull request body content" -a "assignee"

```

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/0f8789dc-ee18-4b0a-9fd4-2670a226c0a8" alt="gh repo pr create"/>

## 4. Review pull requests :eyes:

Run _gh_ pr create and add the necessary flags: -t for the title, -b for the body -a for the assignee and -d to mark the pr as draft. You can also use `gh pr create` and follow the on screen prompts to create a new PR with the cli.
Once the PR is created, take note of the PR num and run `gh pr view <num>` to see the details of the pr in your terminal.

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/9124f58c-7556-4494-9616-4a9c608aa7f9" alt="gh pr review">

## 5. View action workflows :eyes:

We can view the action workflows right from the command line with the _gh_ cli. This command will list all the recent action workflows for the repository.

```bash
gh run list

```

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/cd88ca64-95d2-44f5-bf9e-06d6d05d2da8" alt="gh run list"/>

You can also run workflows right from your terminal with the command `gh workflow run <workflow name>`. This command will run the workflow and prompt you for any required inputs.

```bash
gh workflow run <workflow filename>

```

## 6. Open your current Repo in the browser :computer:

You can open your current repository in the browser right from your terminal. This command will take you to the homepage of your repository.

```bash
gh browse

```

If you you attach the -b option, you can open the current branch in the browser, like so:

```bash
gh browse -b "branch name"

```

## 7. Create and update Project Boards :clipboard:

Creating a project from your terminal with the command line requires you to have project scope permissions. To update your auth token with project scope permissions, run the following command and follow the on screen prompts:

```bash
gh auth refresh -s project,read:project

```

Once that's confirmed, you can create a project board with the command `gh project create` and follow the on screen prompts.

```bash
gh project create --owner "owner" --title "title"

```

## 8. Create edit, and view Releases :rocket:

You can create a release with the command `gh release create` and follow the on screen prompts.

```bash
gh release create --title "title" --notes "notes"

```

You can also edit, dowload assets and delete relases with the _gh_ cli. You can find more information about this [here](https://cli.github.com/manual/gh_release).

## 9. Start and Stop Codespaces :computer:

A Codespace is an online development environment. You can start a codespace with the command `gh codespace create` and follow the on screen prompts. You can also do it by specifying certain flags, for example, the branch you want to use:

```bash
gh codespace create -b "branch"

```

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/02e6e688-53d9-4134-aaef-b07feea39fa2" alt="gh codespace create"/>

You can then open up the codespace in VSCode or Jupyter Notebooks with the command `gh codespace code [flags]` or `gh codespace jupyter [flags]`, respectively, where the flags can include the version of vscode, repo, repo owner and name of the codespace you want to open.

## 10. Browse and create _gh_ cli extensions :package:

You can run `gh extension browse` in your terminal to view a list of all available cli extensions to make your terminal experience even better. You can also install ann extension directly from the browsing interface.

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/2d4e3be9-ece5-404d-b690-3bb9d7354556" alt="gh extension browse command">

To install an extension you like, run `gh extension install <repository> [flags]` and follow the on screen prompts.
You can also create your own cli extensions with the command `gh ext create` and follow the on screen prompts.

## Getting Help with the _gh_ cli :question:

If you're ever unsure of what to do with the GitHub CLI or how to use it, you can always run `gh --help` and it will give you a list of all the commands, help topics, extensions, flags and examples you can run with the GitHub CLI.

<img src="https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/a10819b9-f478-4430-933b-4aa0db58281f" alt="gh --help command"/>

## GitHub Copilot for CLI :robot:

Ever forget a command and have to search for it over and over again? Yea, me too. This is why we built [GitHub Copilot for CLI](https://githubnext.com/projects/copilot-cli). It provides a chat-like interface in your terminal to help you find commands for GitHub CLI, shell commands and git.

I'll be showing you how to use GitHub Copilot for CLI and how amazing it is at [taking your command line skills to the next level](https://reg.githubuniverse.com/flow/github/universe23/sessioncatalog/page/sessioncatalog?search=AI2755D). Join me at GitHub Universe next month to see it in action.

[![github universe copilot for cli talk](https://github.com/LadyKerr/gh-cli-example-repo/assets/47188731/f9e22821-df86-4890-9bf1-bcb27150524d)](https://reg.githubuniverse.com/flow/github/universe23/sessioncatalog/page/sessioncatalog?search=AI2755D)

And if you can't make it in person, virtual passes are free and open to everyone. Register [here](https://githubuniverse.com/).

If you have any questions about the _gh_ cli, feel free to post them below and I'll do my best to answer them. :smile: