1

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:

https://i.sstatic.net/WWTjO.png

Is there any way to fix this? Thanks!

1
  • just rebase B on main should be enough I tink, but not sure to understand your point Commented Sep 17, 2021 at 13:51

1 Answer 1

1

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 A with main, then A has commits A and B which main doesn't have.
  • If you compare B with A, then B has commits C and D which main doesn't have.
  • If you compare B with main, then B has commits A, B, C and D which main doesn'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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.