Skip to content

Instantly share code, notes, and snippets.

@fegue
Created November 12, 2024 10:38
Show Gist options
  • Save fegue/96057e8eed729b13676c276b97dcb013 to your computer and use it in GitHub Desktop.
Save fegue/96057e8eed729b13676c276b97dcb013 to your computer and use it in GitHub Desktop.
Test Pull-Request Merge locally

Check out a pull request locally to see if it would merge without any issues, and even test it without actually merging it into the main branch. Here’s how you can do it:

Step 1: Fetch the Pull Request

First, make sure you have fetched all the pull requests from the remote repository. Replace origin with the name of your remote if it's different.

git fetch origin pull/<PR_NUMBER>/head:<BRANCH_NAME>
  • <PR_NUMBER>: The number of the pull request.
  • <BRANCH_NAME>: A local branch name for this PR (e.g., test-merge-PR123).

For example, if the pull request number is 123, you could do:

git fetch origin pull/123/head:test-merge-PR123

This command fetches the changes from the pull request into a new local branch named test-merge-PR123 without merging anything into your current branch.

Step 2: Check Out the Pull Request Branch

Switch to the branch where you fetched the pull request:

git checkout test-merge-PR123

Step 3: Test the Merge (Optional)

To simulate a merge and see if there are any conflicts with your target branch, you can use git merge --no-commit --no-ff:

git merge main --no-commit --no-ff
  • --no-commit: Prevents the merge from committing immediately.
  • --no-ff: Forces a merge commit to be created, even if it would normally fast-forward.

If there are no conflicts, the merge will be staged but uncommitted, allowing you to inspect the changes without finalizing anything. If conflicts occur, Git will let you know, and you can then resolve them locally if needed.

Step 4: Clean Up (Optional)

To discard the merge test, reset your branch to its original state:

git merge --abort

This command will cancel the merge attempt and leave the branch as it was. You can then delete the branch if you're done testing:

git checkout main
git branch -D test-merge-PR123

This approach lets you safely inspect a pull request, verify if it merges cleanly, and test it without affecting the main branch or the original pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment