Testing Pull Requests Locally with Git Worktrees

Testing Pull Requests Locally with Git Worktrees

Introduction

When reviewing pull requests (PRs), it’s often necessary to run the code locally before approving changes. Instead of old-school branch switching, Git worktrees let you check out a PR into its own directory without touching your current working branch. This, in theory, makes testing PRs a whole lot easier, with the added benefit of a clean main workspace.

Benefits:

  • No branch juggling – Avoid git stash / branch-switching headaches.
  • Lightweight – Multiple working directories share the same Git history.
  • Great for PR reviews – Spin up isolated environments for each PR.

Setup

Using Git worktrees can seem intimidating at first, but once you get the hang of it, you’ll appreciate its benefits. That said, I’ve created a one-time setup guide that you can reuse for your projects for those who want to dive right in. We’ll use aliases to simplify the setup and make it user-friendly.

Run this in your terminal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git config --global alias.pr '!f() {
  num="$1";
  if [ -z "$num" ]; then
    echo "Usage: git pr <pull-request-number>";
    return 1;
  fi;

  branch="pr-$num";
  dir="../pr-$num";
  remote=$(git remote | grep -qx "upstream" && echo "upstream" || echo "origin");

  echo "Fetching PR #$num from remote '\''$remote'\''...";

  git fetch "$remote" "pull/$num/head:refs/heads/$branch" &&
  git worktree add "$dir" "$branch" &&
  echo "Worktree for PR #$num created at $dir";
}; f'

Prerequisites:

  • Git installed and a local clone of the repository.
  • An upstream or origin remote pointing to the GitHub repository containing the PR.
  • Access to the pull request.

The alias automatically prefers upstream when available, falling back to origin otherwise. It fetches the PR into a local branch named pr- and creates a worktree in a sibling directory.


🔹 Usage

Whenever you want to review PR #69:

1
git pr 69

This command will:

  1. Fetch the pull request from the configured remote (upstream if available, otherwise origin).
1
git fetch <remote> pull/69/head:refs/heads/pr-69
  1. Create a new worktree at ../pr-69.
1
git worktree add ../pr-69 pr-69
  1. Print a confirmation message.
    • This keeps the instructions consistent with the alias and avoids implying that every repository uses upstream.

Then you can:

1
cd ../pr-69

…and test/run/review the code safely in isolation.


🔹 Cleanup After Review

When you’re done with the PR:

1
2
3
cd ..
git worktree remove ../pr-69
git branch -D pr-69   # removes the temporary branch

You can verify your remaining worktrees with:

1
git worktree list



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Google Gemini updates: Flash 1.5, Gemma 2 and Project Astra
  • Displaying External Posts on Your al-folio Blog
  • Scraping Historical X (Twitter) Posts via Firefox Console
  • Android Package Conflict
  • How to Set Up Cloudflare Tunnel for Local Development