How can I test a Pull Request before merging it?

Ajeet Singh Raina - Sep 17 - - Dev Community

Managing repositories, pull requests, and authentication via GitHub can sometimes feel cumbersome, especially if you prefer working from the command line. The GitHub CLI (command-line interface) simplifies many of these tasks by allowing you to perform GitHub-related operations directly from your terminal.

This deep dive will focus on how you can use the GitHub CLI to authenticate your GitHub account, list pull requests (PRs), and check out a specific PR, all with a few simple commands.

Prerequisite

  • Install GitHub CLI:

MacOS

brew install gh
Enter fullscreen mode Exit fullscreen mode

Ubuntu

sudo apt update
sudo apt install gh
Enter fullscreen mode Exit fullscreen mode

Fedora

sudo dnf install gh
Enter fullscreen mode Exit fullscreen mode

Arch Linux

sudo pacman -S github-cli
Enter fullscreen mode Exit fullscreen mode

Step 1: Authentication with GitHub CLI

The first step in using the GitHub CLI is to authenticate your account. The CLI provides an easy and secure way to log in without needing to manually configure authentication tokens or SSH keys. Here’s how:

1. Install the GitHub CLI

If you haven’t already, install the GitHub CLI by following the official installation guide. You can install it on macOS, Linux, or Windows.

2. Login Using gh auth login

Once installed, you can authenticate by running the command:

gh auth login
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted with several options:

  • Choose the GitHub account: You will select GitHub.com if you're logging into a regular GitHub account.
  • Preferred Git protocol: Choose HTTPS, as it simplifies authentication with your GitHub credentials.
  • GitHub CLI Authentication: Select to authenticate via a web browser.

After making these selections, the CLI will present a one-time code to be entered into your browser for secure authentication:

! First copy your one-time code: 0XXC-09XC
Press Enter to open github.com in your browser...
Enter fullscreen mode Exit fullscreen mode

Once you authenticate in the browser, you will see the message indicating a successful login:

✓ Authentication complete.
Enter fullscreen mode Exit fullscreen mode

The CLI also configures Git with your chosen protocol:

- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as <your-username>
Enter fullscreen mode Exit fullscreen mode

That’s it! You are now authenticated and ready to interact with GitHub using the CLI.

Step 2: Listing Open Pull Requests

Pull requests are the heart of collaboration on GitHub. The GitHub CLI makes it incredibly simple to manage and review PRs without needing to leave your terminal.

To list all open pull requests for the repository you are working on, run:

gh pr list
Enter fullscreen mode Exit fullscreen mode

This command will return a list of open PRs in your repository. Here’s an example output:

Showing 3 of 3 open pull requests in kubetoolsio/krs-docker-extension

#4  Update UI krs                         update-ui-krs         about 16 minutes ago
#3  Update krs extension                  update-krs-extension  about 4 days ago
#2  Add Docker Desktop Extension for KRS  add-krs-extension     about 7 days ago
Enter fullscreen mode Exit fullscreen mode

The GitHub CLI fetches PR details such as:

  • PR number (#4, #3, etc.)
  • Title of the PR
  • Branch name
  • Time since the PR was opened

This makes it easy to get an overview of ongoing work in the repository.

Step 3: Checking Out a Specific Pull Request

Once you’ve identified a PR that you’d like to review or work on, checking it out is just a single command away. The GitHub CLI lets you seamlessly switch to the branch associated with any pull request by using the following command:

gh pr checkout <pr-number>
Enter fullscreen mode Exit fullscreen mode

For instance, to check out PR #4, you would run:

gh pr checkout 4
Enter fullscreen mode Exit fullscreen mode

The CLI automatically fetches and checks out the branch associated with that PR, allowing you to inspect, test, or modify the code.

Why Use the GitHub CLI?

While you could achieve many of these tasks using the GitHub web interface or Git commands, the GitHub CLI provides significant benefits:

  • Efficiency: No need to leave the terminal, making your workflow faster and more efficient.
  • Integration: Seamlessly integrates with Git and other tools, reducing context switching.
  • Automation: The CLI can be easily scripted for repetitive tasks, making it ideal for automating parts of your development process.

Conclusion

The GitHub CLI simplifies working with GitHub repositories by allowing you to perform actions like authentication, listing pull requests, and checking out PRs directly from your terminal. Whether you’re managing contributions to an open-source project or collaborating on code in a team, using the GitHub CLI can make your workflow smoother and more efficient.

Now that you know how to authenticate, list, and check out PRs with GitHub CLI, try it out in your development environment to see how it fits into your daily routine!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player