Merge Conflict Resolu2on public float calculateDiscount( final float - - PowerPoint PPT Presentation

merge conflict resolu2on
SMART_READER_LITE
LIVE PREVIEW

Merge Conflict Resolu2on public float calculateDiscount( final float - - PowerPoint PPT Presentation

Merge Conflict Resolu2on public float calculateDiscount( final float cost) { <<<<<<< HEAD return cost * 0.5f; ======= return cost / 2; >>>>>>> dev1 } SWEN-261 Introduc2on to So3ware Engineering


slide-1
SLIDE 1

SWEN-261 Introduc2on to So3ware Engineering

Department of So3ware Engineering Rochester Ins2tute of Technology

Merge Conflict Resolu2on

public float calculateDiscount(final float cost) { <<<<<<< HEAD return cost * 0.5f; ======= return cost / 2; >>>>>>> dev1 }

slide-2
SLIDE 2

Merging happens on every pull. § Every time you sync with the remote a merge

  • ccurs.
  • A "fast forward" is when your local branch has no

additional changes and your local ref is just moved to the commit point of the remote.

  • If your local changes do not overlap with the

changes fetched from the remote, then an automatic merge is possible.

§ A conflict occurs when there is at least one file with overlapping changes.

2

slide-3
SLIDE 3

Here's an example of a merge conflict. § Imagine there is a bug in a method:

/** * Calculate a half-off discount. */ public float calculateDiscount(final float cost) { return cost * 2; }

§ Independently, two developers fix this bug:

  • Betty did this:

return cost / 2;

  • Sam did this:

return cost * 0.5f;

3

slide-4
SLIDE 4

When a conflict occurs git reports the affected files. § When Sam merges in the code from Betty:

➔ git merge dev1 Auto-merging src/main/java/com/example/model/Promotion.java CONFLICT (content): Merge conflict in src/main/java/com/example/ model/Promotion.java Automatic merge failed; fix conflicts and then commit the result.

§ Sam now has to fix the conflict; seen here:

public float calculateDiscount(final float cost) { <<<<<<< HEAD return cost * 0.5f; ======= return cost / 2; >>>>>>> dev1 }

4

The HEAD in Sam's workspace. This is the code from Betty's branch.

slide-5
SLIDE 5

Resolving a simple text conflict is often easy. § Determine the best solution, and remove the other solution and the marker text.

/** * Calculate a half-off discount. */ public float calculateDiscount(final float cost) { return cost / 2; }

§ Then add (and commit) the resolved change:

git commit –am "Fixed discount calculation"

§ Push it.

5

slide-6
SLIDE 6

There are other challenges when merging. § Binary files

  • Document files, such as Word or Excel, are binary

and cannot be merged.

  • Such files should have an owner who is responsible

for moderating changes.

  • And a clear protocol for permitting team members

access.

§ Tree structures

  • Types:

w file name changes w directory name changes w moving files and directories

  • Why:

w Java package changes w Class name changes

6

slide-7
SLIDE 7

To minimize the number of times when conflicts will not resolve easily, follow several guidelines.

  • 1. Keep code lines short; break up long

calculations.

  • 2. Keep commits small and focused.
  • 3. Minimize stray edits.
  • 4. If multiple developers are collaborating on a

feature, each developer should sync with the remote branch regularly.

Merge in the remote branch and then push to it, if you have changes.

  • 5. If development of a feature is taking a long time,

merge in master to sync with completed features in this sprint.

7