1

I have 1 a git/github question.

I forked from a master branch, name it fb, made updates to some files locally, git pushed fb onto github, and submitted a pull request on github (for a code review). Another colleague also submitted another PR, for code review, got approvals, and just merged (rebased, with commits squash) his branch into the stable (his now becomes the new stable (I mispoke, saying master before).

Like with my colleague, my submitted PR also had several commits that I did on github (not on local via git). Because I forked, I only have 1 branch on local, which was what got pushed and submitted for a pull request. What's the best way to merge my forked fb branch into my colleague's stable branch?

Should I follow this process: a pull from my remote github fb repo to get all of my commits into local fb, create another local branch called stable, do a pull from the new github master/stable onto the local stable branch, rebase my fb to the local stable, resolve conflicts, then do a push origin --force to the remote master/stable?

pdn2020
  • 91
  • 1
  • 8
  • Thanks for the response. Let me clarify. The original master wasn't gone. With my friend's merging of his branch, his branch now becomes stable. I need to merge my fb branch into this new stable. I am limited to using git, not github with merging. Problem is, my local fb is few commits behind my github's fb repo. – pdn2020 May 09 '20 at 23:12
  • I'm a little confused. The `stable` branch is not `master`? If so you want to submit your commits as a PR to `stable`, not `master`? That's atypical, but possible. You need to put yourself in a stranger's shoes and update your question so that that stranger can understand it :) – Inigo May 10 '20 at 03:11

1 Answers1

1

step 1: Sync up you local master to upstream/master

You probably should get your local master synced up to upstream first. If you have commits on your local master that are not in upstream/master, then "save" them by creating a new branch ref on the same commit. Once you've done that (if you needed to), hard reset your local master to the latest upstream/master.

(You might find How do I update a GitHub forked repository? useful. Also How to avoid merge-commit hell on GitHub/BitBucket)

I get the sense you already know how, but just in case:

git checkout master
git checkout -b SAVE    // only do this if you need to 
                        // save unmerged work on local master
git fetch upstream master
git reset --hard upstream/master

step 2: Get the/a branch with the commits you want to PR rebased onto master

If you want to update your existing PR, do this on your fb branch. (You can also do it on a new branch. Your call. But the following instructions assume fb)

Get all the commits you want to submit on fb and rebase fb onto the now up-to-date master. (you can do it in the other order, depending on what's easier. I don't have the details to decide for you).

Once it all looks good, force push fb to origin. This will update your original PR automatically with the new rebased commits.

For example the following assumes that the changes you made on GitHub were made on the fb branch there. If not, adjust the commands and the sequence as needed.

git checkout fb
git pull origin/fb    //to grab the commits you made directly on GitHub
git rebase --onto master <the commits on fb you want to rebase>
git push -f origin fb  

The last command above will overwrite origin/fb, so make sure you've already gotten everything you want off of it (e.g. when you previously pulled it into your local fb)

Inigo
  • 12,186
  • 5
  • 41
  • 70
  • Thanks Inigo. So, this is the process then? 1) pull from my remote fb, 2) create a new branch on local, and pull from remote stable into this local stable branch; 3) rebase (also squash, since I want to merge all my commits into 1) my fb to the local stable branch; 4) git push -f origin fb – pdn2020 May 10 '20 at 02:42
  • I'm not sure why you need to create a new branch (I don't know all the details). I update my answer with more details. Step 1 you should definitely do pretty much as I laid it out. Step 2 depends on details I don't know. Maybe you do need to create a new branch and do your work on that to keep things straight. Whatever you do, get all your commits the way you want them and rebased onto master. Make sense? – Inigo May 10 '20 at 03:07
  • Added some useful links to my answer. – Inigo May 10 '20 at 03:16
  • Yes, it does. Thanks so much Inigo! – pdn2020 May 10 '20 at 03:16
  • Hi Inigo, after further discussions with my colleagues, I went ahead using the Squash and Merge option on github, which I didn't know was allowed. There were no conflicts with my merge, so not much work was needed besides merging the history comments. In fact, it was such an ease, with this approach compared to git commands (basing on your feedbacks). I didn't get a chance to your steps, I wouldn't doubt they wouldn't work. So I appreciate your time writing those steps for me! I did the upvote BTW, but it won't show since I have less than 15 reputations :) Not sure how to accept your answer – pdn2020 May 11 '20 at 16:58
  • Sorry, I don't see what you are describing :( – pdn2020 May 11 '20 at 18:40