0

I always have an issue when I got this scenario in my work

0 <- 1 <- 2 <----------- 9 <--- master
           \            /
            3 <- 4 <- 5 <------ Feature/A
                       \
                        6 <- 7 <- 8   <-- HEAD=Feature/B

so we have a master, we created new feature/A from master and later we created another feature feature/B from feature/A

now Feature/A got merged to master at 5

we need to merge Feature/B to master too, but we are getting tons of conflicts for no reason.

any idea what is causing that please , and how I can avoid this?

Edit

To merge B into master I run

git merge master

from Feature B I ran git merge-base --all HEAD master and the result was 2

which is releae/1.14.1 as you can see it in more detail on the next graph

my graph log command :

git log --graph --oneline master feature/member feature/fund-test

as feature/member is Feature A

feature/fund-test is Feature B

releae/1.14.1 Is where feature/A started which is 2

7640e46aa IS 9

|\
| * c354b9142 update claims upon registering.
|/
*   7640e46aa Merge branch 'feature/members' into 'master'
|\
| * 43ea27cb2 Feature/Members
|/
| *   ef276ed89 (HEAD -> fund-test, origin/fund-test) Merge branch 'minor-changes-fundraising-v3.1' into 'feature/fundraising-3.1'
| |\
| | * 952cfe3a9 (origin/add-missing-changes) Add changes resolve by git.
| |/
| * e924e1fc3 fixed changelog
| *   1d6eadffe Merge remote-tracking branch 'origin/feature/members' into feature/fundraising-3.1
| |\
| | * 76d3f1aa4 (origin/feature/members, feature/members) Update data service comments
| | * 5b8cdf931 Update NuGet packages
| | *   cf978c86b Merge branch 'master' into feature/members
| | |\
| |_|/
|/| |
* | |   2754d5127 Merge branch 'releae/1.14.1' into 'master'
|\ \ \
| * | | d67a38076 Update changelogs
|/ / /
| | *   aa16154e4 Merge branch 'remove/context' into 'feature/members'
Aiman
  • 33
  • 3
  • *tons of conflicts for no reason."* Well without knowing what exactly you've done here this question can't be answered. There **will** be a reason. But that very much depends on what you've done in the files in these branches – Liam Oct 01 '21 at 08:21
  • 2
    I assume 9 is a merge commit? Just to be clear, the feature A branch wasn't squashed when it was merged, or rebased? Both of those would cause lots of fun conflicts. – SpoonMeiser Oct 01 '21 at 08:28
  • are you merging master first to featureB ? Also, what is the point to create featureB from featureA ? By looking at your diagram it is better to start featureB from master – Dmitry Oct 01 '21 at 09:36
  • @Dmitry , some time you have two feature depending on each other, you can't start B from master as feature A can have a stuff that gonna be used in feature B – Aiman Oct 01 '21 at 10:55
  • @SpoonMeiser 9 is merge commit yes , feature A has only push and merge into master at the end , nothing else . for some reason when I merge B to master it compare ,2 ,8 and 9 , and [2] will be the base on ( 3 way merge ) – Aiman Oct 01 '21 at 10:57
  • Can you let us know what commands you ran? If feature A has been merged into master, then 2 shouldn't be part of a 3-way merge – SpoonMeiser Oct 01 '21 at 13:01
  • I run "git merge master" – Aiman Oct 02 '21 at 09:28
  • I updated the question with more details , thanks a lot guys for the help anyway :D – Aiman Oct 02 '21 at 09:28
  • See https://stackoverflow.com/questions/29914052/how-to-git-rebase-a-branch-with-the-onto-command/29916361#29916361 – Gaël J Oct 02 '21 at 09:33

1 Answers1

3

Edit: given the note that hash ID 7640e46aa is the commit you drew as 9, we look at the two parents of 7640e46aa. They are:

43ea27cb2 Feature/Members

(this is the second parent, as git log --graph always draws that one to the right) and:

2754d5127 Merge branch 'releae/1.14.1' into 'master'

(this is the first parent, directly below the commit you called 2). So the top row of your graph is correct. However, commit 43ea27cb2 Feature/Members does not correspond to the commit you called 5: it is an independent commit with 2754d5127 as its (single) parent. That is, rather than:

0 <- 1 <- 2 <----------- 9 <--- master
           \            /
            3 <- 4 <- 5 <------ Feature/A
                       \
                        6 <- 7 <- 8   <-- HEAD=Feature/B

what we have looks like this:

0--1--2------------9   <-- master
       \___       /
        \  `---345   <-- Feature/Members
         \
          3--4--5   <-- Feature/A
                 \
                  6--7--8   <-- Feature/B

where commit 345 is the result of running git checkout -b Feature/Members <hash-of-2> && git merge --squash Feature/A rather than git checkout master && git merge Feature/A. The squashed-together 345 was then merged with a regular merge into master, and the branch name Feature/Members (note upper case) deleted. This branch was thus not related to the feature/members (note lower case) branch and commits 3, 4, and 5 conflict with commit 345.

Your primary set of choices now lie between merging anyway (use a real merge if at all possible!) and resolving the conflicts, or use git rebase --onto to copy commits 6-7-8 to three new commits. The advantage to using rebase is that the job will be easier. The disadvantage is that the three replacement commits for 6-7-8 must be used as replacements in every clone that currently has those three commits.

(original answer below the line)


Assuming your drawing is accurate, the merge base of commits 8 and 9 is 5. To verify this, run:

git merge-base --all HEAD master

This should print just that one commit's hash ID.

Conflicts will arise if/when changes that show up in the diff from 5 to 8 overlap or abut with changes that show up in the diff from 5 to 9. That is, having obtained the merge base's hash ID (singular), run:

git diff --find-renames <hash-of-5> feature/B > /tmp/ours
git diff --find-renames <hash-of-5> master > /tmp/theirs

Compare the diff listings to see where conflicts might occur.

Since the merge at 9 was supposedly made by git checkout master && git merge --no-ff feature/A, and there are no commits between 9 and its first parent 2, the changes that show up here, from 5 to 9, should be exactly those changes that were made along the 3-4-5 path. This means the diff from 5 to 9, in /tmp/theirs, should be empty. An empty diff cannot produce conflicts (with a non-empty or empty diff), so that either of:

git checkout master && git merge feature/B

or:

git checkout feature/B && git merge master

should have no conflicts. Therefore I conclude that at least one assumption here is wrong. The most likely one to be wrong is the assumption that your simplified commit graph drawing is correct. Consider editing your question to include the output of git log --graph --oneline master feature/A feature/B (possibly with some of the commit message subject lines edited out, if necessary). However, another possibility is that the simplified graph drawing is correct, but the merge at 9 is an evil merge: see Evil merges in git? In this case the /tmp/theirs diff will be non-empty.

(My own guess is that commit 9 is a squash merge, and has no link back to commit 5, so that the merge base is actually commit 2.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • thanks a lot @torek for all this, I did update my question with the command you asked, I deleted some of stuff that I think it's not needed from the output as we have lots of commits there . I hope I didn't delete anything useful , – Aiman Oct 02 '21 at 09:10
  • we are using GitLab for merge , it squash the commits and merge => then it will make 9 – Aiman Oct 02 '21 at 09:11
  • 1
    @Aiman, then you need to rebase featureB onto master. The actual command to avoid conflicts will be `git rebase --onto`. – Gaël J Oct 02 '21 at 09:30
  • See https://stackoverflow.com/questions/29914052/how-to-git-rebase-a-branch-with-the-onto-command/29916361#29916361 – Gaël J Oct 02 '21 at 09:33
  • the problem on the rebase you have to resolve the conflicts commit by commit , if you have hundreds of commits that will be nightmare – Aiman Oct 02 '21 at 09:49
  • my problem is I can't see why we are getting conflicts as B has everything in 5 and 5 got merged , so I should assume 0 conflicts – Aiman Oct 02 '21 at 09:51