GitHub
pull requests
command line
git
version control

Can you issue pull requests from the command line on GitHub?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you can create and manage GitHub pull requests entirely from the command line. The typical workflow uses Git for branch operations and GitHub CLI gh for PR lifecycle actions. This approach is fast, scriptable, and useful when you want to stay in terminal-based workflows.

Install and Authenticate GitHub CLI

First confirm gh is installed and authenticated.

bash
gh --version
gh auth login

Validate repository context from inside your local clone:

bash
git remote -v
git branch --show-current
gh repo view

This prevents accidental PR creation in the wrong repository.

Create a Pull Request from Terminal

A common sequence:

bash
1git checkout -b feature/add-export-endpoint
2# edit files
3git add .
4git commit -m "Add export endpoint"
5git push -u origin feature/add-export-endpoint

Create PR interactively:

bash
gh pr create

Or non-interactively with explicit metadata:

bash
1gh pr create \
2  --base main \
3  --head feature/add-export-endpoint \
4  --title "Add export endpoint" \
5  --body "Implements CSV export route with request validation."

Explicit flags are best for automation and repeatable team workflows.

Add Reviewers, Labels, and Draft State

You can set PR metadata at creation time.

bash
1gh pr create \
2  --base main \
3  --head feature/add-export-endpoint \
4  --title "Add export endpoint" \
5  --body "Adds endpoint and tests" \
6  --reviewer alice,bob \
7  --label backend \
8  --label api \
9  --draft

Using labels and reviewers early reduces manual follow-up.

Track and Update PR from Command Line

Useful commands after opening PR:

bash
1gh pr status
2gh pr view
3gh pr diff
4gh pr checks

When review comments arrive, update branch as usual:

bash
git add .
git commit -m "Address review feedback"
git push

The existing PR updates automatically because it is branch-backed.

Merge PR from Terminal

After approvals and passing checks:

bash
gh pr merge --squash --delete-branch

Alternative strategies:

bash
gh pr merge --merge
gh pr merge --rebase

Use the strategy required by repository policy.

Open PR in Browser When Needed

Terminal workflow does not prevent browser use when richer UI is helpful.

bash
gh pr view --web

This command opens the current PR page directly, which is useful for checking review threads or code-owner requirements.

Use PR Templates and Body Files

If your team uses detailed templates, pass body from file.

bash
gh pr create \
  --title "Fix cache eviction race" \
  --body-file .github/pull_request_template.md

Structured PR descriptions improve review quality and release traceability.

Scripted PR Creation for Repetitive Tasks

Command-line PR creation is especially valuable for release or dependency automation.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4branch="chore/dependency-refresh"
5git checkout -b "$branch"
6# run update commands here
7git add .
8git commit -m "Refresh dependencies"
9git push -u origin "$branch"
10
11gh pr create \
12  --base main \
13  --head "$branch" \
14  --title "Dependency refresh" \
15  --body "Automated dependency updates and lockfile refresh."

This pattern is common in bot-assisted maintenance workflows.

Security and Access Considerations

Ensure your authentication method has required scopes for PR operations, review requests, and merge actions.

If operating in enterprise environments:

  • use short-lived tokens where possible
  • keep CLI auth tied to least-privileged accounts
  • rotate credentials regularly

For shared machines, always run gh auth status before sensitive actions.

Common Pitfalls

A common pitfall is creating a PR before pushing the branch. Push first or specify --head explicitly.

Another issue is forgetting --base, which can target an unintended default branch.

Teams also confuse Git operations with GitHub operations. git push moves commits, while PR creation is a GitHub API action handled by gh.

Auth scope mismatches can silently block reviewer assignment or merge commands.

Finally, scripted workflows without branch checks can open duplicate PRs; include idempotency checks when automating.

Summary

  • GitHub pull requests can be fully created and managed from terminal.
  • Use Git for branch changes and gh for PR lifecycle commands.
  • Include base, head, title, and body explicitly for consistent results.
  • Use gh pr status and gh pr checks to monitor PR health.
  • Command-line PR workflows are excellent for both daily work and automation.

Course illustration
Course illustration

All Rights Reserved.