The first thing to understand is that pull requests are not at all a Git feature. They are something that certain Git hosting sites do, and they do it in different ways.
Let's confine our considerations to GitHub. GitHub handles this situation in a very convenient and coherent way.
For purposes of the example, let's say you have a branch branchA
and you submit it to GitHub as a pull request to be merged into main
. You then make a new branch branchB
off the end of branchA
and you continue development there. This happens to me a lot, so I have pretty deep experience of how it works.
There are two possible ideal subsequent outcomes:
Ideal path 1
The first ideal thing that might happen is that while you are still working on branchB
, the PR for branchA
is accepted and merged. Then you simply rebase branchB
onto main
, like this:
git rebase --onto main branchA branchB
That incantation rips the branchB
commits off the point where branchB
diverged from branchA
and tacks them on to the end of main
. You continue working until you are ready to submit the PR, and you submit it to be merged into main
. Since branchB
now comes directly off main
, the PR will consist only of the commits that you've made on branchB
, which is just what you want.
Observe that this works just fine even if more commits were added to branchA
as part of its acceptance.
Ideal path 2
Now let's say that, instead, you finish your branchB
work while the branchA
PR has not yet been accepted. No problem; go ahead and submit the PR for branchB
to GitHub, but when you do, as you construct the new PR in the GitHub web interface, tell GitHub that you want to merge branchB
into branchA
(not into main
). By doing that, two wonderful things happen:
The PR for branchB
consists only of the commits you made on branchB
, which is just what you want.
If the branchA
PR is now accepted and merged, GitHub magically changes the branchB
PR so that it is now asking to merge into main
! Moreover, the PR still consists only of the commits you made on branchB
!
That, too, works just fine even if branchA
acquired some more commits as part of its acceptance. In that case, however, after branchA
PR is accepted and merged, you might want to merge main
into branchB
("reverse merge") just to pick up (and perhaps reconcile) the latest changes.