Created
May 26, 2025 13:30
-
-
Save th3nolo/f1fa03796895b4a868b1c81cf8fe6f63 to your computer and use it in GitHub Desktop.
Helper to fetch & test PR branches locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # --------------------------------------------------------------------------- | |
| # checkout_pr_branch.sh · Quick‑test any PR branch locally | |
| # --------------------------------------------------------------------------- | |
| # Given the name of a remote branch (e.g. the head branch of a Pull Request), | |
| # this script will: | |
| # 1. Fetch that branch from the chosen remote (default: "origin"). | |
| # 2. Create (or reset) a local branch that tracks the remote branch. | |
| # 3. Switch you to the new local branch so you can run / build / test. | |
| # | |
| # --------------------------------------------------------------------------- | |
| # Usage | |
| # ./checkout_pr_branch.sh <branch-name> [remote] [local-alias] | |
| # | |
| # <branch-name> – The head branch of the PR (e.g. feat/migration-alert-message) | |
| # [remote] – Git remote to fetch from (default: origin) | |
| # [local-alias] – Optional different name for the local branch | |
| # | |
| # Examples | |
| # # 1. Typical workflow – same name locally & remote, remote is origin | |
| # ./checkout_pr_branch.sh feat/migration-alert-message | |
| # | |
| # # 2. Remote PR in upstream and you want to call your local branch "test‑pr" | |
| # ./checkout_pr_branch.sh feat/xyz upstream test-pr | |
| # --------------------------------------------------------------------------- | |
| set -e # Exit on first error | |
| if [[ -z "$1" ]]; then | |
| echo "Usage: $0 <branch-name> [remote] [local-alias]" | |
| exit 1 | |
| fi | |
| BRANCH="$1" | |
| REMOTE="${2:-origin}" | |
| LOCAL_ALIAS="${3:-$BRANCH}" | |
| # Verify remote exists ------------------------------------------------------- | |
| if ! git remote | grep -qx "$REMOTE"; then | |
| echo "❌ Remote '$REMOTE' not found. Add it first: git remote add $REMOTE <url>" | |
| exit 1 | |
| fi | |
| echo "➜ Fetching '$BRANCH' from '$REMOTE' …" | |
| git fetch "$REMOTE" "$BRANCH" | |
| echo "➜ Creating/updating local branch '$LOCAL_ALIAS' to track '$REMOTE/$BRANCH' …" | |
| git checkout -B "$LOCAL_ALIAS" --track "$REMOTE/$BRANCH" | |
| echo "✅ Done! You are now on branch '$LOCAL_ALIAS'. Happy testing!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment