Git & GitHub - GitHub CLI
Introduction to the GitHub Command Line Interface
The GitHub Command Line Interface (CLI) is a powerful tool that allows you to interact with GitHub directly from your terminal. With GitHub CLI, you can manage repositories, issues, pull requests, and more, without needing to switch to the web interface.
Key Points:
- GitHub CLI simplifies your workflow by providing a way to interact with GitHub from the terminal.
- You can perform many tasks such as creating issues, managing pull requests, and cloning repositories.
- GitHub CLI supports custom scripts and aliases to automate repetitive tasks.
Installing GitHub CLI
For macOS
Use Homebrew to install GitHub CLI:
# Install GitHub CLI using Homebrew
$ brew install gh
For Windows
Use the Windows installer from the GitHub CLI releases page:
# Download and run the Windows installer
$ msiexec.exe /i https://github.com/cli/cli/releases/latest/download/gh_2.0.0_windows_amd64.msi
For Linux
Use the package manager for your distribution (e.g., apt for Debian/Ubuntu, dnf for Fedora, yum for CentOS):
# Install GitHub CLI on Debian/Ubuntu
$ sudo apt update
$ sudo apt install gh
# Install GitHub CLI on Fedora
$ sudo dnf install gh
# Install GitHub CLI on CentOS
$ sudo yum install gh
Authenticating with GitHub CLI
After installing GitHub CLI, authenticate it with your GitHub account:
# Authenticate with GitHub CLI
$ gh auth login
Follow the prompts to complete the authentication process. You can choose to authenticate using a web browser or by providing a personal access token.
Common GitHub CLI Commands
Here are some common GitHub CLI commands to get you started:
- Clone a repository:
gh repo clone owner/repo
- Create a new issue:
gh issue create
- View issues:
gh issue list
- Create a pull request:
gh pr create
- View pull requests:
gh pr list
- Merge a pull request:
gh pr merge
- Check notifications:
gh notification
# Example: Clone a repository
$ gh repo clone octocat/Hello-World
# Example: Create a new issue
$ gh issue create --title "Bug report" --body "There is a bug in the application."
# Example: Create a pull request
$ gh pr create --title "Fix issue" --body "This PR fixes the issue." --base main --head feature-branch
Customizing GitHub CLI
You can customize GitHub CLI by creating aliases for frequently used commands and writing custom scripts:
Creating Aliases
Create aliases to shorten long commands:
# Create an alias for listing issues
$ gh alias set il 'issue list'
# Use the alias
$ gh il
Writing Custom Scripts
Write custom scripts to automate repetitive tasks. Save the script as a file and run it using GitHub CLI:
# Example script to list open pull requests and merge them
#!/bin/bash
gh pr list --state open --json number --jq '.[].number' | while read pr; do
gh pr merge $pr --merge
done
# Make the script executable
$ chmod +x merge-open-prs.sh
# Run the script
$ ./merge-open-prs.sh
Summary
This guide provided an introduction to the GitHub Command Line Interface (CLI), covering installation, authentication, common commands, and customization. GitHub CLI is a powerful tool that enhances your workflow by allowing you to manage your GitHub projects directly from the terminal.