Software Development

A Guide to Using GitHub from the Command Line with GitHub CLI

GitHub is the world’s most popular platform for hosting and collaborating on software projects. Traditionally, we interact with GitHub through its web interface — creating pull requests, managing issues, or reviewing code online. However, for developers who prefer working directly from the terminal, GitHub CLI (Command Line Interface) offers a faster and more efficient alternative.

This article explores what the GitHub CLI is, how to install it, and how to use it for common GitHub operations without ever leaving your command line.

1. What Is GitHub CLI?

GitHub CLI (gh) is an open-source command-line tool developed by GitHub that allows us to interact with GitHub repositories and perform tasks such as creating issues, reviewing pull requests, cloning repositories, and managing workflows — all from the terminal.

In essence, it brings GitHub’s web-based features to our local development environment, enabling a seamless workflow between coding and collaboration.

Key benefits of GitHub CLI include:

  • Speed: Perform GitHub operations faster without switching to a browser.
  • Integration: Works with Git, CI/CD pipelines, and shell scripts.
  • Automation: Ideal for scripting and automating repository management.
  • Consistency: Same commands across operating systems (Windows, macOS, Linux).

2. Installing GitHub CLI

GitHub CLI can be installed on all major operating systems. Here’s how to set it up:

Windows

winget install --id GitHub.cli

Or using Chocolatey

choco install gh

macOS

brew install gh

Linux

For Debian/Ubuntu-based distributions:

sudo apt install gh

You can verify installation by running:

gh --version

If successful, this command will display the installed version of the GitHub CLI.

3. Authenticating with GitHub

Before using gh, you need to authenticate your GitHub account. Run:

gh auth login

You’ll be prompted to:

  • Choose between GitHub.com or GitHub Enterprise.
  • Select your preferred protocol (HTTPS or SSH).
  • Open a browser to authorize access or paste an authentication token.

Once authenticated, you can confirm with:

gh auth status

This displays the authenticated user and account details.

4. Common GitHub CLI Commands

GitHub CLI supports a wide range of commands. Below are some of the most frequently used ones.

Cloning and Forking Repositories

GitHub CLI simplifies cloning and forking repositories directly from the terminal.

Clone a Repository

Instead of using git clone, you can do:

gh repo clone owner/repository-name

Example:

gh repo clone torvalds/linux

This command automatically clones the repository into your current directory.

Fork a Repository

To fork a repository to your GitHub account:

gh repo fork owner/repository-name

Create a New Repository

To create a new repository from your terminal:

gh repo create my-new-repo

You’ll be asked whether you want the repository to be public or private, and whether to add files like .gitignore or README.md.

Managing Branches and Pull Requests

Branches and pull requests are central to GitHub collaboration, and the CLI provides intuitive commands to handle them.

Create a Branch

git checkout -b feature/new-feature

List All Branches

gh repo view --web

This opens the repository in your browser, or you can use:

git branch

Pushing and Pulling Changes

GitHub CLI complements Git but doesn’t replace it. You’ll still use standard Git commands for version control.

Pull the Latest Changes

git pull origin main

Push Local Changes

git push origin feature/new-feature

After pushing, you can immediately create a pull request using:

gh pr create

This combination of Git and GitHub CLI creates a seamless workflow from commit to collaboration.

Interacting with Releases and Tags

GitHub CLI also allows you to create, view, and manage releases or tags.

List Releases

gh release list

Create a New Release

gh release create v1.1.0 --notes "Added new performance improvements."

Create and View Issues

You can create new issues directly from the CLI:

gh issue create

This command opens an interactive prompt to enter a title, description, and labels. To list existing issues:

gh issue list

To view details of a specific issue:

gh issue view 25

Here, 25 is the issue number.

Manage Pull Requests

Creating and managing pull requests is one of the most powerful features of the GitHub CLI. Create a pull request:

gh pr create --base main --head feature-branch --title "Add new feature" --body "This PR adds the new feature."

List all pull requests:

gh pr list

Check details or review a pull request:

gh pr view 10

Merge a pull request from the terminal:

gh pr merge 10 --merge

Manage Workflows (GitHub Actions)

You can view and monitor CI/CD workflows:

gh workflow list

Trigger a workflow manually:

gh workflow run build.yml

View recent workflow runs:

gh run list

To see all available commands, run:

gh help

5. Automating Tasks with GitHub CLI

GitHub CLI works seamlessly with shell scripts and automation tools. For instance, you can write a script to automate daily maintenance tasks like labeling issues or merging PRs automatically.

Example script to close inactive issues:

for issue in $(gh issue list --state open --label "stale" --json number -q '.[].number'); do
  gh issue close $issue --comment "Closing stale issue due to inactivity."
done

This demonstrates how gh can be used to automate routine repository management tasks.

Using GitHub CLI with Git

GitHub CLI doesn’t replace Git — instead, it complements it. You can still use traditional Git commands (git add, git commit, git push), while gh helps with repository operations that require interaction with GitHub itself (e.g., creating pull requests or viewing issues).

This combination allows us to manage both source control and GitHub collaboration without context switching.

6. Updating and Uninstalling GitHub CLI

Keeping your CLI updated ensures you have the latest features and bug fixes.

On macOS (Homebrew)

brew upgrade gh

On Windows (Winget)

winget upgrade --id GitHub.cli

On Linux

sudo apt update && sudo apt upgrade gh

To check your version:

gh --version

To uninstall:

brew uninstall gh   # macOS
winget uninstall GitHub.cli  # Windows

7. Conclusion

The GitHub CLI streamlines your workflow by bringing the power of GitHub directly to your terminal. Whether you’re managing issues, creating pull requests, or monitoring CI/CD pipelines, the CLI provides a developer-friendly way to stay productive without ever leaving the command line.

8. Download the Source Code

This article explored how to use GitHub from the command line.

Download
You can download the full source code of this example here: how to use github from the command line

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button