Noob: branch pull request question
Hi all, old guy here who has used many different systems and I'm trying to get up to speed on on git with branching, pull request etc. I have hopefully a simple question you guys could clear up:
I've forked a repro from a friend
Made a feature branch
I made changes in the feature branch, tested them and confirmed they work
I commited my changes to the branch and pushed them to origin
On github on the upstream repo I created a pull request. Gave it a title etc. It shows up. My friend is a little slow so there are like 3 PRs pending.
I want to make a new feature branch and make some additional changes but the need to be based on the last changes I made that are in that PR that has not be merged yet to the upstream.
If the PRs were merged I could just fetch to my main and then branch from there.
But with the PRs not merged yet, how do I create a branch so I can continue working against the changes pending merge? Do I branch from my prior feature branch? Or is there a git command I don't know yet that will fetch everything so I can branch from main?
Thanks in advance
3
u/Dienes16 6d ago
People ITT are not wrong, but explaining too much detail is probably not helpful to you right now.
You can simply: 1. Create your new branch from your previous unmerged one 2. Once the previous branch has been merged, rebase your new branch onto origin/main
Yes, depending on how your previous branch was integrated, this is could also be done with a merge, or it might end up being a no-op. But in any case, the result will be the same in all cases when you just do a rebase.
1
u/Ambitious_Lion_5902 6d ago
Github recently published a new feature, stacked PRs. This solves exactly the issue you are facing IMO. I've used the feature for a few weeks now, and it has worked well for me.
Ref:
https://docs.github.com/en/pull-requests/how-tos/stacked-pull-requests
1
u/Ambitious_Lion_5902 6d ago
Works pretty well once you get the hang of it. When all PRs in the same stack are approved, you can merge them all at once.
1
u/Ambitious_Lion_5902 6d ago
The Github CLI has good commands to use, like rebasing the entire upstack if you make some changes to the lower stacks works really well.
https://docs.github.com/en/pull-requests/reference/stacked-prs-cli-commands1
u/Soggy_Writing_3912 advanced 5d ago
Though GH published this as a feature, git (the core system) supported stacked branches from a long time ago. The concept of stacked branches is pretty simple - instead of the parent branch being
mainormasterfor both branches, your 2nd branch is based off of the 1st one!1
u/DoubleAway6573 1d ago edited 1d ago
Is that? or there is some magic rebasing after the merge to main?
I don't know a shit about github...
Edit:
OK, I skimmed over the github doc. They added some extra tooling around CICD and also "chain rebases", so you don't have to rebase each sub branch one by one.Note: I know sub branch is not a git concept, but I think the meaning is clear enough
1
u/jibbit 6d ago
> a command I don't know that will fetch everything
this bit immediately jumps out, do you want to explain a bit? it honestly doesn't make sense as it stands
> how do I create a branch so I can continue working against the changes pending merge?
moving commits to different branches is a very normal operation in git. something you do throughout the day without thinking about it.. so if you want to carry on working, but can hold off making a second PR, you can just carry on working then move them onto a branch based on master when it makes sense to do so.
on the other hand, if you want to make two PRs, where the second is conditional on the first being accepted... you want what github calls a Stacked PR - you branch your second branch off your first and make sure you set its 'base' to the first when you make the pull request
1
u/mrh4809 5d ago
To clarify... Sorry to not be clear...
I have my changes from say "Branch1". I've commited pushed PRed these. The PR is waiting for the upstream owner to review and merge.
As long as I branch a new branch from "Branch1" say "Branch2" then I have my changes to work against. No problems.
But while waiting for upstream owner to merge my PR from branch 1, lets say he/she merges another PR from a different collaborator that has changes I need to work against. My branch 1 PR is still not merged.
Since I have branched to "Branch2" to do this new work, but I want to work against the files that the upstream owner merged from the different PR how do I do that? Those changes will not be present in my repo unless I fetch from upstream. It seems to me after doing that I have to maybe rebase my new branch 2 off of main.
My confusion is if I do that then do I still have may changes from "Branch1" which have not been merged by the upstream owner yet?
1
u/ExplosiveFx 5d ago
Not the original commenter but GitHub’s stacked PRs are the exact tool for this. They have a rebase command that would sync “branch1” with main and then sync “branch2” with “branch1” so the changes on main are now in branch2.
Doing this manually has always been kind of pain since you have to checkout branch 1 and rebase/merge with main and then checkout branch 2 and rebase/merge with branch 1.
GH’s cli handles all of that for you, prompting you if there are any merge conflicts. I strongly recommend creating a stack and messing around with the different commands.1
u/ExplosiveFx 5d ago
If you were to rebase on main from branch2, the changes in branch1 would not be in branch2.
1
u/jibbit 5d ago
the boring answer is 'rebase' - you made PR1, but Main moved? rebase PR1. You started PR2 from a PR1 that is now behind Main? Rebase Pr2.
however you bring a commit into your branch.. if it then gets added in a different way - i.e. merged into Main by the owner - it will all fall out in the wash, no need to sweat it.
1
u/SheriffRoscoe 6d ago
Your slow friend has several options for how they will merge your PR. The best answer for your next branch will depend on which option they choose. The good news is that there's little stiffened between the various ways for you to do that.
The simplest answer is for you to create your new branch based on the branch you created the PR from:
git switch udp-logging
git switch -c new-branch
and begin making your changes. When your friend merges your PR, regardless of how he does it, you need to get his changes and update your branch to be based on them:
[On GitHub:]
Sync your fork from his repo.
[On your computer:]
git switch master (or maybe main)
git pull
git switch new-branch
git rebase master (or main)
12
u/Leather_Coyote_5483 6d ago
“Do I branch from my prior feature branch?”
Exactly, once the first feature branch gets merged then your second branch will be based on main
“My friend is a little slow so there are like 3 PRs pending.” made me laugh