DEV Community

Cover image for How to Create a Pull Request from the Terminal Using GitHub CLI
Cristian Jonhson Alvarez
Cristian Jonhson Alvarez

Posted on

How to Create a Pull Request from the Terminal Using GitHub CLI

If you work with Git every day, switching back and forth between your terminal and GitHub's web interface can interrupt your workflow.

Fortunately, GitHub CLI (gh) allows you to create, review, and manage Pull Requests directly from the terminal. In this guide, you'll learn how to create a Pull Request from a feature branch to main without ever leaving your command line.

Prerequisites

Before getting started, make sure you have:

  • Git installed
  • GitHub CLI installed
  • A GitHub account
  • Authenticated GitHub CLI

Authenticate with GitHub:

gh auth login
Enter fullscreen mode Exit fullscreen mode

Verify your authentication:

gh auth status
Enter fullscreen mode Exit fullscreen mode

1. Create a Feature Branch

Start by updating your local main branch:

git checkout main
git pull origin main
Enter fullscreen mode Exit fullscreen mode

Create a new feature branch:

git checkout -b feature/add-user-service
Enter fullscreen mode Exit fullscreen mode

Or using the newer Git syntax:

git switch -c feature/add-user-service
Enter fullscreen mode Exit fullscreen mode

2. Make Your Changes

After implementing your changes, check your working tree:

git status
Enter fullscreen mode Exit fullscreen mode

Stage and commit your changes:

git add .
git commit -m "Add user service implementation"
Enter fullscreen mode Exit fullscreen mode

3. Push the Branch to GitHub

Push the branch to the remote repository:

git push -u origin feature/add-user-service
Enter fullscreen mode Exit fullscreen mode

The -u flag establishes tracking between your local and remote branch.


4. Create the Pull Request

Option 1: Explicit Branch Names

Create a Pull Request specifying both source and target branches:

gh pr create \
  --base main \
  --head feature/add-user-service \
  --title "Add user service implementation" \
  --body "This PR adds the user service and related business logic."
Enter fullscreen mode Exit fullscreen mode

Option 2: Automatically Detect the Current Branch

A cleaner approach is to let Git determine the current branch:

gh pr create \
  --base main \
  --head $(git branch --show-current) \
  --title "Add user service implementation" \
  --body "This PR adds the user service and related business logic."
Enter fullscreen mode Exit fullscreen mode

This is especially useful when working with multiple feature branches.

Option 3: Interactive Mode

GitHub CLI can guide you through the process:

gh pr create
Enter fullscreen mode Exit fullscreen mode

You will be prompted to:

  • Select the base branch
  • Select the head branch
  • Enter a title
  • Enter a description

Option 4: Open GitHub's PR Form in Your Browser

If you prefer GitHub's web interface while keeping the CLI workflow:

gh pr create --web
Enter fullscreen mode Exit fullscreen mode

This command opens the Pull Request page in your browser with most fields already prefilled.


5. View the Pull Request

List all Pull Requests:

gh pr list
Enter fullscreen mode Exit fullscreen mode

View details of the current Pull Request:

gh pr view
Enter fullscreen mode Exit fullscreen mode

Open the Pull Request in your browser:

gh pr view --web
Enter fullscreen mode Exit fullscreen mode

Optional: Merge the Pull Request

Once your Pull Request has been approved, you can merge it directly from the terminal.

Standard merge:

gh pr merge
Enter fullscreen mode Exit fullscreen mode

Squash merge and delete the feature branch:

gh pr merge --squash --delete-branch
Enter fullscreen mode Exit fullscreen mode

Regular merge and delete the feature branch:

gh pr merge --merge --delete-branch
Enter fullscreen mode Exit fullscreen mode

Complete Example

git checkout main
git pull origin main

git checkout -b feature/add-user-service

git add .
git commit -m "Add user service implementation"

git push -u origin feature/add-user-service

gh pr create \
  --base main \
  --head $(git branch --show-current) \
  --title "Add user service implementation" \
  --body "This PR adds the user service and related business logic."

gh pr view --web
Enter fullscreen mode Exit fullscreen mode

Why Use GitHub CLI for Pull Requests?

Using GitHub CLI offers several advantages:

  • Faster workflow
  • Less context switching
  • Easy automation in scripts
  • Better productivity for terminal-first developers
  • Seamless integration with Git and GitHub

For developers who spend most of their day in the terminal, GitHub CLI is one of the simplest ways to streamline the Pull Request process.

Conclusion

GitHub CLI makes Pull Request creation simple, fast, and efficient. Whether you prefer a fully terminal-based workflow or a hybrid approach with the browser, gh pr create provides multiple ways to open Pull Requests with minimal effort.

Once you get used to it, you'll rarely need to manually navigate GitHub's interface just to create a Pull Request.


Top comments (0)