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
Verify your authentication:
gh auth status
1. Create a Feature Branch
Start by updating your local main branch:
git checkout main
git pull origin main
Create a new feature branch:
git checkout -b feature/add-user-service
Or using the newer Git syntax:
git switch -c feature/add-user-service
2. Make Your Changes
After implementing your changes, check your working tree:
git status
Stage and commit your changes:
git add .
git commit -m "Add user service implementation"
3. Push the Branch to GitHub
Push the branch to the remote repository:
git push -u origin feature/add-user-service
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."
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."
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
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
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
View details of the current Pull Request:
gh pr view
Open the Pull Request in your browser:
gh pr view --web
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
Squash merge and delete the feature branch:
gh pr merge --squash --delete-branch
Regular merge and delete the feature branch:
gh pr merge --merge --delete-branch
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
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)