A bash/zsh function that automatically checks out the newest open Pull Request from your GitHub repository. This script simplifies the workflow of testing and reviewing the latest PR by automatically switching to the PR branch.
- π Automatically detects the newest open Pull Request
- β‘ Quick checkout with a single command
- π Falls back to standard
git pullif no PR is found - π¦ Works with GitHub CLI (
gh) - π― Smart branch switching to
mainbefore PR checkout - π Auto-rebase: Automatically rebases the PR branch onto the latest base branch (e.g.,
mainormaster) - π€ Auto-push: Automatically pushes the rebased branch to GitHub after successful rebase
Before using this script, ensure you have:
-
GitHub CLI (
gh) installed and authenticated# Install GitHub CLI (macOS) brew install gh # Authenticate with GitHub gh auth login
-
Git installed (usually pre-installed on macOS/Linux)
-
Bash or Zsh shell
-
Clone this repository:
git clone https://github.com/abdlkrmdlgr/claudeBranchPuller.git cd claudeBranchPuller -
Run the installer:
./install.sh
The installer will:
- β Auto-detect your shell (bash/zsh)
- β
Add
gprto your shell configuration - β Create a backup of your config file
- β Prompt you to reload your shell
-
Important: If your default branch is not
main(e.g.,master,develop, etc.), you'll need to modifygpr.shbefore running the installer. See the Customization section below. -
Reload your shell or restart your terminal:
# For Zsh source ~/.zshrc # For Bash source ~/.bashrc
That's it! The gpr command is now available.
If you prefer manual installation or the automatic installer doesn't work for your setup:
-
Clone or download this repository:
git clone https://github.com/abdlkrmdlgr/claudeBranchPuller.git cd claudeBranchPuller -
Make the script executable:
chmod +x gpr.sh
-
Important: If your default branch is not
main(e.g.,master,develop, etc.), you'll need to modify the script before installation. See the Customization section below. -
Add the function to your shell configuration:
For Zsh (macOS default):
echo "source $(pwd)/gpr.sh" >> ~/.zshrc source ~/.zshrc
For Bash:
echo "source $(pwd)/gpr.sh" >> ~/.bashrc source ~/.bashrc
-
Open your shell configuration file:
- Zsh:
~/.zshrc - Bash:
~/.bashrc
- Zsh:
-
Copy and paste the entire
gpr()function fromgpr.shinto your configuration file. -
Save the file and reload your shell:
# For Zsh source ~/.zshrc # For Bash source ~/.bashrc
Simply navigate to any Git repository and run:
gpr- Switches to the default branch (default:
main- see Customization if your default branch has a different name) - Checks for the newest open Pull Request using GitHub CLI
- If a PR is found:
- Displays the PR number and branch name
- Automatically checks out the PR branch
- Automatically rebases the PR branch onto the latest base branch (e.g.,
mainormaster) - This ensures the PR branch includes any changes that were merged to the base branch after the PR was opened
- Automatically pushes the rebased branch to GitHub using
--force-with-leasefor safety
- If no PR is found:
- Falls back to running
git pullon the current branch
- Falls back to running
Note: The script assumes your default branch is named
main. If your repository usesmaster,develop, or another branch name as the default, you'll need to customize the script. See the Customization section below.
When a PR is found:
π Checking for the newest open Pull Request...
PR Info: 42 feature/new-feature
π Newest PR found: #42 (feature/new-feature)
β¬οΈ Fetching the PR locally and switching to this branch...
β
PR #42 successfully checked out. You are currently on branch 'feature/new-feature'.
π Rebasing 'feature/new-feature' onto 'main'...
β
Successfully rebased onto 'main'. Branch is now up-to-date!
π€ Pushing rebased branch to GitHub...
β
Successfully pushed rebased branch to GitHub!
When no PR is found:
π Checking for the newest open Pull Request...
PR Info:
β
No new active open PR was found.
π Running standard 'git pull' command...
- Make sure GitHub CLI is installed:
brew install gh - Verify installation:
gh --version - Authenticate:
gh auth login
- This means your repository doesn't have a branch named
main. The script defaults tomain, but your repository might usemaster,develop, or another branch name. - Solution: Customize the script to use your default branch name. See the Customization section below.
- Make sure you're in a Git repository directory
- Initialize a Git repo if needed:
git init
- Your Git version might be outdated. Update Git:
brew install git
- Alternatively, replace
git switch mainwithgit checkout mainin the script
- Make sure you've reloaded your shell:
source ~/.zshrcorsource ~/.bashrc - Verify the function is loaded:
type gpr - Check if the script path is correct in your configuration file
- If the PR branch has conflicts with the base branch, the rebase will pause
- You'll see a message: "
β οΈ Rebase encountered conflicts. Please resolve them manually." - To resolve:
- Fix the conflicts in the affected files
- Stage the resolved files:
git add <file> - Continue the rebase:
git rebase --continue
- To abort the rebase and return to the pre-rebase state:
git rebase --abort
- If the automatic push to GitHub fails, you'll see a warning message with the manual push command
- Common causes:
- Network connectivity issues
- Insufficient permissions to push to the repository
- Remote branch has been updated by someone else (protected by
--force-with-lease)
- To push manually after a failed automatic push:
git push --force-with-lease origin <branch-name>
- Note: The script uses
--force-with-leaseinstead of--forcefor safety. This ensures you don't accidentally overwrite changes that were pushed to the remote branch while you were rebasing.
The script defaults to main branch. If your repository uses a different default branch name (like master, develop, trunk, etc.), you must modify the script before using it.
To change the default branch, edit the script file (gpr.sh) or your shell configuration file and modify the following line:
# Change this line in gpr.sh:
git switch main
# To your default branch, e.g.:
git switch master
# or
git switch develop
# or
git switch trunkExample: If your default branch is master:
# Before:
git switch main
# After:
git switch masterAfter making this change, reload your shell configuration:
source ~/.zshrc # or source ~/.bashrcTo change the PR search criteria, modify the gh pr list command:
# Example: Search for PRs by a specific author
PR_INFO=$(gh pr list --state open --author "@me" --limit 1 ...)
# Example: Search for PRs with specific labels
PR_INFO=$(gh pr list --state open --label "ready-for-review" --limit 1 ...)This project is open source and available under the MIT License.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
Created for simplifying GitHub PR checkout workflow.
Note: This script requires active internet connection to query GitHub API via gh CLI.