1

I'm quite new to Git and GitHub, so please help me with this question!

I want to contribute to a github project, so I did the following steps:

  • I forked the original repository (the so called upstream, right?)
  • I git cloned my fork locally (git clone 'myforkrepo')
  • I created a new branch to work on a new feature (git switch -c myfeatureA)
  • I also created a new branch from main to work on another separate feature (git switch main and the git switch -c myfeatureB)
  • I was working on my local branches when I realised that, meanwhile, the upstream main was updated!
  • I read about Syncing a fork - GitHub Docs and I added the original repo as upstream, switched to my local main and **merged **(git merge upstream/main) (Or should I rebase?).

Now, my question is: since I need to update all my local branches to the upstream main in order to make pull requests, do I have to merge upstream/main with each local branch? Or do I have to merge the local main with all the branches? And How to update my remote fork?

JBach
  • 513
  • 1
  • 4
  • 8
  • before you open PR you need to be aligned with the master branch to avoid conflicts do the following to achieve that `git fetch upstream` to get the latest updates from the remote `git rebase upstream/master` to sync your branch with the master `git push -f` to push the changes. – odaiwa Dec 05 '22 at 20:24
  • @odaiwa thanks for your answer. Do the rebase command align also all the feature branches or only the forked main? – JBach Dec 05 '22 at 20:45
  • 1
    updates the current branch, if you want to update other branches you need to execute the same commands on it. – odaiwa Dec 06 '22 at 08:00

1 Answers1

1

do I have to merge upstream/main with each local branch? Or do I have to merge the local main with all the branches?

It depends if you plan to:

  • create 2 independent pull requests from the feature branches, or
  • create 1 pull request with the work from both feature branches (i.e. merge the feature branches into your main and then create a PR from your main

If you plan to have to separate PR with 1 feature each then you should integrate the upstream target branch (not always main) into your feature branches. It's entirely OK to keep your feature branches in sync with upstream with or without going via your main.

The main branch is not special per se and if you are not planning to use your main branch there is no requirement to keep it in sync with upstream. Your main is only special if you make it special.


It's worth remembering that there is no technical requirement to merge the target->feature before you merge feature->target; especially if changes are done in different parts of the code. You can just merge feature->target and use this merge to see if there are conflicts.

Merging target->feature first allows us detecting conflicts early, but also it allows to test the codebase with changes from both branches before the actual feature->target merge. Sometimes two changes - even if they are in different parts of the code base (i.e. no conflicts) - don't work well together.

tymtam
  • 31,798
  • 8
  • 86
  • 126