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
- macOS :To install GitHub CLI on macOS, you can use Homebrew:
brew install gh
- Windows :For Windows users, you can use Scoop:
scoop install gh
- Linux :For Debian-based systems (like Ubuntu), use:
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.
gh auth login
It will look like:
Basic Commands
Repository Management
Cloning a Repository - Cloning a repository using gh is straightforward:
gh repo clone owner/repo-name
This command is equivalent to git clone but integrates with other GitHub CLI features.
Creating a Repository
To create a new repository
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.
Pull Requests
Creating a Pull Request
To create a pull request from the current branch:
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:
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:
gh pr list
To view details of a specific pull request:
gh pr view PR_NUMBER
Merging a Pull Request
To merge a pull request:
gh pr merge PR_NUMBER
You can specify the merge method (merge, squash, rebase) using flags:
gh pr merge PR_NUMBER --squash
Issues
Creating an Issue
To create a new issue:
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:
gh issue create --title "Bug report" --body "Description of the bug"
Viewing Issues
To list all open issues:
gh issue list
To view a specific issue:
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
gh workflow list
Running a Workflow
To manually trigger a workflow:
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:
gh alias set myalias 'command'
For example, to create an alias for listing all open pull requests:
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!