简体   繁体   中英

Merge conflicts when using squash merge

My team and I have been using git for about a year--none of us had any prior experience with git or any other version control. We do most of our work in dev, and when we're ready, we make a release branch, make any changes if needed, then merge the release branch into master. When merging our release branches into master, we've been doing a squash merge to keep the commit history clean. We've read lots of guides/tutorials/how-tos and everyone one of them says something different, but this is what we decided to do.

What I have noticed is whenever we merge our release branch into master, we always get merge conflicts. Not with every file, but with about 15% of them or so. Most of these don't seem like they should be conflicts, but they show up as one. Here's an example:

Master branch before merge:

<div style="float:right">
    <strong>Select Report: </strong>
    <select name="report" id="report">
        <option value="">-- SELECT REPORT --</option>
        <optgroup label="General">
            <option value="aganalysis_stats"<?php if($report == 'aganalysis_stats') echo " selected"; ?>>AgAnalysis Stats</option>
        </optgroup>
        <optgroup label="LSPs">
            <option value="lsps_pending_approval_for_current_quarter"<?php if($report == 'lsps_pending_approval_for_current_quarter') echo " selected"; ?>>LSPs Pending Approval for Current Quarter</option>
            <option value="members_requiring_lsps"<?php if($report == 'members_requiring_lsps') echo " selected"; ?>>Members Requiring LSPs</option>
            <option value="missing_lsps_for_current_quarter"<?php if($report == 'missing_lsps_for_current_quarter') echo " selected"; ?>>Missing LSPs for Current Quarter</option>
        </optgroup>
        <optgroup label="UCCs">
            <option value="uccs_by_branch"<?php if($report == 'uccs_by_branch') echo " selected"; ?>>UCCs by Branch</option>
            <option value="uccs_eligible_for_renewal"<?php if($report == 'uccs_eligible_for_renewal') echo " selected"; ?>>UCCs Eligible for Renewal</option>
            <option value="uccs_expired"<?php if($report == 'uccs_expired') echo " selected"; ?>>Expired UCCs</option>
        </optgroup>
    </select>
</div>

Master branch after merge:

<div style="float:right">
    <strong>Select Report: </strong>
    <select name="report" id="report">
        <option value="">-- SELECT REPORT --</option>
        <optgroup label="Appraisal Requests">
            <option value="appraisal_request_stats">Appraisal Request Stats</option>
        </optgroup>
        <optgroup label="General">
            <option value="aganalysis_stats">AgAnalysis Stats</option>
        </optgroup>
        <optgroup label="LSPs">
            <option value="lsps_pending_approval_for_current_quarter">LSPs Pending Approval for Current Quarter</option>
            <option value="members_requiring_lsps">Members Requiring LSPs</option>
            <option value="missing_lsps_for_current_quarter">Missing LSPs for Current Quarter</option>
        </optgroup>
        <optgroup label="UCCs">
<<<<<<< HEAD
            <option value="uccs_by_branch"<?php if($report == 'uccs_by_branch') echo " selected"; ?>>UCCs by Branch</option>
            <option value="uccs_eligible_for_renewal"<?php if($report == 'uccs_eligible_for_renewal') echo " selected"; ?>>UCCs Eligible for Renewal</option>
            <option value="uccs_expired"<?php if($report == 'uccs_expired') echo " selected"; ?>>Expired UCCs</option>
=======
            <option value="uccs_by_branch">UCCs by Branch</option>
            <option value="uccs_eligible_for_renewal">UCCs Eligible for Renewal</option>
            <option value="uccs_expired">Expired UCCs</option>
>>>>>>> refs/remotes/origin/dev
        </optgroup>
    </select>
</div>

As you can see, I made the exact same changes on both "LSPs" and "UCCs" optgroups. But, I only get a merge conflict in the "UCCs" optgroup. Why am I getting this merge conflict? Am I merging incorrectly? Should I not use a squash merge when merging branches into master? I've read so many things about rebasing, squash commits, no fast-forward, etc., I don't know what's right or wrong anymore.

Let me try to answer these for you... Hopefully it helps.

Why am I getting this merge conflict?

Because git has found that what is trying to merge is so different that it needs you to intervene and tell it what code should be there. The options it gave you are what git has on master (above the ======= ) and what is in your branch (below the ======= ).

You can choose either one or make additional changes, save the file, and commit the file with a new message (generally something along the lines of "fixing merge conflict by...").

Am I merging incorrectly?

Nope. Git just found a merge that it couldn't automatically handle due to dramatic differences in the two branches it was trying to merge.

Should I not use a squash merge when merging branches into master?

This will vary based on git user to git user, but I generally try to squash commits before I merge the branch into master. This generally helps because then you're merging fewer commits, but won't always solve the problem of merge conflicts, as these happen when branches differ. Therefore, if master has changed from a third branch which touches the same code as your branch, you'll likely still have a merge conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM