Checking out Git PRs on your local machine it’s not so uncommon practice, especially if you want to test things out before they are merged into the main branch or any other branch that could screw your environment easily that you might care of. Let’s see how is done from the CLI.
Prerequisites
- Git
From the Terminal/CMD
Step 1. Open Terminal, fetch the Pull Requests and create a new branch on the fly.
git fetch origin pull/<PR_ID>/head:<SOME_BRANCH_NAME>
Example:
git fetch origin pull/136/head:main
Step 2. Now, checkout to the new branch.
git checkout <SOME_BRANCH_NAME>
Step 3 (Optional). If you want to push the local branch to the remote repo, run:
git push origin <SOME_BRANCH_NAME>
Using the GitHub CLI
Step 1. List all pull requests.
gh pr list
Step 2. Checkout a PR.
gh pr checkout 136
Conclusion
If you wish to see more, check the following official GitHub Checking out pull requests locally docs. To find more neat Git commands and hacks, browse the Git category. On a side note, follow our official channel on Telegram.