Skip to content

GitHub CLI: Managing Repositories from the Terminal

DodaTech Updated 2026-06-22 5 min read

In this tutorial, you'll learn about GitHub CLI: Managing Repositories from the Terminal. We cover key concepts, practical examples, and best practices.

The GitHub CLI (gh) is a command-line tool for managing issues, pull requests, repos, and Actions directly from your terminal without switching to the browser.

In this tutorial, you'll learn the GitHub CLI — gh — which brings GitHub workflows to your terminal. Instead of switching between your editor, terminal, and browser, you manage issues, PRs, repos, and Actions from one place. By the end, you'll handle common GitHub tasks without leaving the command line.

flowchart LR
  A[Terminal] --> B[gh auth login]
  B --> C[gh repo create]
  B --> D[gh issue list]
  B --> E[gh pr create]
  B --> F[gh run list]
  C --> G[gh repo clone]
  D --> H[gh issue view 42]
  E --> I[gh pr review]
  F --> J[gh run watch]

Installation and Authentication

# Install on Linux
sudo apt install gh
# or macOS: brew install gh

# Authenticate
gh auth login

Expected output:

? What account do you want to log into? GitHub.com
? How would you like to authenticate? Login with a web browser
! First copy your one-time code: ABCD-1234
Press Enter to open github.com in your browser...
✓ Authentication complete.

Repository Management

# Create a new repo from current directory
gh repo create my-project --public --push

# Clone a repo
gh repo clone your-username/my-project

# View repo details
gh repo view your-username/my-project

# Fork a repo
gh repo fork original-owner/project --clone=true

Issue Management

# List issues
gh issue list

# List issues with filters
gh issue list --label bug --assignee @me

# View an issue
gh issue view 42

# Create an issue
gh issue create --title "Fix login bug" --body "Description of the bug" --label bug

# Close an issue
gh issue close 42

# Reopen an issue
gh issue reopen 42

Expected output from gh issue list:

Showing 5 of 5 open issues in your-username/my-project

#42  Fix login bug             (bug)     about 2 hours ago
#41  Add payment feature       (feature) about 1 day ago
#40  Update documentation      (docs)    about 3 days ago

Pull Request Management

# Create a PR
gh pr create --title "Add login feature" --body "Implements OAuth2 login" --assignee @me

# List PRs
gh pr list

# Checkout a PR locally
gh pr checkout 42

# View PR details
gh pr view 42

# Review a PR
gh pr review 42 --approve
gh pr review 42 --comment --body "Looks good, just one minor issue"

# Merge a PR
gh pr merge 42 --squash

# Close without merging
gh pr close 42

Working with Gists

# List your gists
gh gist list

# Create a gist
gh gist create app.py --public --desc "Example app"

# View a gist
gh gist view GIST_ID

# Edit a gist
gh gist edit GIST_ID app.py

GitHub CLI in Automation Scripts

Use gh in shell scripts for automation:

#!/bin/bash
# auto-release.sh - Create a release from CI

VERSION=$(jq -r .version package.json)
TITLE="Release v$VERSION"
NOTES=$(git log $(git describe --tags --abbrev=0)..HEAD --oneline)

gh release create "v$VERSION" \
  --title "$TITLE" \
  --notes "$NOTES" \
  --target main

Actions and Workflows

# List recent workflow runs
gh run list

# View a specific run
gh run view 123456789

# Watch a run in real-time
gh run watch 123456789

# Rerun a failed workflow
gh run rerun 123456789

# Download artifacts
gh run download 123456789

Releases

# List releases
gh release list

# Create a release
gh release create v1.0.0 --title "v1.0.0" --notes "Initial release"

# Download release assets
gh release download v1.0.0

Common Errors

Error Cause Fix
gh: command not found CLI not installed Install GitHub CLI
You are not logged in No authentication Run gh auth login
HTTP 404 on repo operations Wrong repo name or no access Check repo name and permissions
gh pr create fails with no repo Not in a git repo Run from inside a repository
Must have push access No write permission Fork the repo and push to fork
Operation canceled Interactive prompt timed out Use --non-interactive flag
gh run watch stuck Workflow not started Check workflow triggers
Argument required Missing required flag Check command syntax with --help

Practice Questions

What is the GitHub CLI?

The GitHub CLI (gh) is a command-line tool that provides GitHub functionality in your terminal. It supports authentication, repo management, issues, pull requests, Actions, releases, and more. It reduces the need to switch between terminal and browser during development.

How do I create a pull request from the command line?

Run gh pr create from inside the repository on your feature branch. Add --title and --body flags for the PR description. Use --assignee @me to self-assign and --label to add labels.

How do I approve a PR via CLI?

Run gh pr review PR-NUMBER --approve. Replace PR-NUMBER with the PR number. You can also use --comment to leave feedback without approving, or --request-changes to block the PR.

Can I manage GitHub Actions with gh?

Yes. gh run list shows recent workflow runs, gh run view shows details, gh run watch streams logs in real-time, and gh run rerun restarts failed runs. You can also download artifacts with gh run download.

Is the GitHub CLI available in CI/CD?

Yes. GitHub Actions runners have the gh CLI pre-installed. It's authenticated via the GITHUB_TOKEN environment variable. You can use gh commands in workflow steps to automate GitHub operations

Challenge

Create a repository on GitHub using gh repo create. Clone it locally, create a feature branch, make a change, commit, and push. Create an issue for a bug. Create a pull request that references the issue. List all issues and PRs. Approve and merge the PR. Create a release for the merged changes. Delete the local and remote branch.

Real-World Task

Write a shell script that automates the standard contribution workflow: (1) Syncs your fork with upstream using gh repo sync, (2) Creates a feature branch, (3) Opens a PR with a template description, (4) Assigns reviewers, (5) Waits for CI using gh run watch, (6) Merges when approved. This automation is used at DodaTech to standardize the contribution workflow across all teams working on Durga Antivirus Pro.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro