Supercharge Your Workflow: A Deep Dive into GitHub CLI

4 min read
Supercharge Your Workflow: A Deep Dive into GitHub CLI

GitHub CLI

GitHub CLI (Command Line Interface), often referred to as gh, is a powerful tool that allows developers to interact with GitHub directly from their terminal. This guide will walk you through the essentials of GitHub CLI, from installation to advanced usage, helping you integrate it seamlessly into your development workflow.

Introduction to GitHub CLI

GitHub CLI simplifies your GitHub workflow by providing command-line access to GitHub features. Whether you're managing repositories, creating pull requests, or reviewing issues, gh enables you to perform these actions without leaving your terminal.

Installation

  1. macOS :To install GitHub CLI on macOS, you can use Homebrew:
Terminal
brew install gh
  1. Windows :For Windows users, you can use Scoop:
Terminal
scoop install gh
  1. Linux :For Debian-based systems (like Ubuntu), use:
Terminal
sudo apt install gh

You can also download the binary from the GitHub CLI releases page.

Authentication

Before using GitHub CLI, you need to authenticate it with your GitHub account. This is a one-time setup process.

Terminal
gh auth login

It will look like:

595fd7b9-0e84-471e-ab4d-3e92903ab3c1.png

Basic Commands

Repository Management

Cloning a Repository - Cloning a repository using gh is straightforward:

Terminal
gh repo clone owner/repo-name

This command is equivalent to git clone but integrates with other GitHub CLI features.

18d75d4f-5aeb-41be-b4fb-d93ec34d47cf.png

Creating a Repository

To create a new repository

Terminal
gh repo create my-new-repo

You'll be prompted to provide details like visibility (public or private) and whether to add a README file.

4ae95a6e-48a9-471c-9616-94a3b0bc2a06.png

Pull Requests

Creating a Pull Request

To create a pull request from the current branch:

Terminal
gh pr create

You'll be prompted to enter a title, body, and other details. You can also specify these details directly in the command:

Terminal
gh pr create --title "My new feature" --body "Description of my new feature"

Viewing Pull Requests

To list all open pull requests in the current repository:

Terminal
gh pr list

To view details of a specific pull request:

Terminal
gh pr view PR_NUMBER

Merging a Pull Request

To merge a pull request:

Terminal
gh pr merge PR_NUMBER

You can specify the merge method (merge, squash, rebase) using flags:

Terminal
gh pr merge PR_NUMBER --squash

Issues

Creating an Issue

To create a new issue:

Terminal
gh issue create

You’ll be prompted to provide a title and body for the issue. Like with pull requests, you can specify these details directly:

Terminal
gh issue create --title "Bug report" --body "Description of the bug"

Viewing Issues

To list all open issues:

Terminal
gh issue list

To view a specific issue:

Terminal
gh issue view ISSUE_NUMBER

Managing Workflows

GitHub CLI also supports interaction with GitHub Actions workflows.

Listing Workflows

To list workflows in the current repository

Terminal
gh workflow list

Running a Workflow

To manually trigger a workflow:

Terminal
gh workflow run WORKFLOW_NAME

Advanced Usage

Custom Aliases

GitHub CLI allows you to create custom aliases for commands to streamline your workflow.

To create an alias:

Terminal
gh alias set myalias 'command'

For example, to create an alias for listing all open pull requests:

Terminal
gh alias set prlist 'pr list'

Now, you can use gh prlist instead of gh pr list.

The official documentation https://docs.github.com/en/github-cli serves as a valuable resource, providing detailed instructions for using various gh commands and exploring advanced functionalities.

Conclusion

Git CLI's primary objective is to "minimize context switching" by allowing you to continue inside your terminal/console, rather than opening your browser to access GitHub.

GitHub CLI is a powerful tool that empowers developers to interact with GitHub effectively from the command line. Its ability to streamline workflows, boost productivity, and offer a familiar development environment makes it an indispensable addition to any developer's toolkit. So, dive into the world of GitHub CLI and unleash the full potential of your development workflow!

Follow us on social media

Cyber Unfolded Light Logo
Copyright © 2024 CYUN. All rights reserved.