Home > Net >  How can I preview a PR merge result in github before actually merging it?
How can I preview a PR merge result in github before actually merging it?

Time:01-23

Let's suppose you have a GitHub PR already approved, and you want to merge it. However, it is made of several commits. Let's suppose you want to squash them before merging.

Is there any way of previewing how the git history would end after the the merge-squash operation, before actually doing it?

CodePudding user response:

You can check out the pull request locally and git merge --squash it into another branch. The easiest way to check out a pull request locally is using the gh cli. If you wanted to check out PR #100, you would run:

gh pr checkout 100

This will create a local branch named after the source branch of the pull request.

You can accomplish the same thing with just git. The following would check out PR #100 as example_branch locally:

git fetch origin pull/100/head:example_branch

Once you have a local branch, you can check out a target branch and merge it. For example, if we want to try merging it into main:

git checkout main
git merge --squash <branch_name>
git commit -m "test sqash merge"

Where <branch_name> is the branch that contains your pull request.

If you didn't want to modify your main branch locally, you could create a temporary branch to test the operation:

git checkout -b test_branch main
git merge --squash <branch_name>
git commit -m "test sqash merge"
  • Related