I have a branch main and a branch A based off main that has some commits.
If I now create a new branch B based off A, create more commits on B and during that time, branch A gets merged into main, then on GitHub, you'll likely see duplicate code when checking the B branch pull request into main branch. As seen here, this is the B pull request, there are the commits from both A branch and B branch:

Is there any way to fix this? Thanks!
CodePudding user response:
Let's visualize your scenario:
o---o (main)
\
A---B (A)
\
C---D (B)
In Git, a branch is simply a reference to the latest of a sequence of commits. What commits are in a branch depends on which branches you're comparing:
- If you compare
Awithmain, thenAhas commitsAandBwhichmaindoesn't have. - If you compare
BwithA, thenBhas commitsCandDwhichmaindoesn't have. - If you compare
Bwithmain, thenBhas commitsA,B,CandDwhichmaindoesn't have.
Now, if branch A were to be merged into main, then main would also have commits A and B. This means that if you compare branch B with main after the merge, then B would have commits C and D that aren't in main:
o---o-------M (main)
\ /
A---B (A)
\
C---D (B) <-- these commits aren't in 'main'
The fact that GitHub is showing you commits from branch A when you do a pull request from branch B to main makes me think that branch A was rewritten (possibly rebased) after you created branch B:
o---o---o--------M (main)
\ \ /
\ A'---B' (A) <-- rebased commits in branch 'A'
\
A---B---C---D (B) <-- branch 'B' still has the old commits
If this is indeed the case, the solution is simply to rebase branch B on top of main and update the pull request:
o---o---o--------M (main)
\ / \
A'---B' \
^ \
(A) C---D (B)
Git will notice that the changes from commits A and B are already in main thanks to commits A' and B' and will therefore only show you commits C and D when you compare branch B to main.
