2.4. Merge Conflicts#

Merge conflicts arise when people on separate branches modify the same parts of one (or multiple) files. Since Git does not know how to handle that and whose changes to consider, it prompts the user to decide instead.

https://github.com/TUDelft-MUDE/source-files/raw/main/file/mconflict1.png

Fig. 2.9 Visualization of a merge conflict#

Git and GitHub assist you in solving these conflicts, as seen in the figure below:

Attribution

License: CC BY 4.0 | Copyright: © 2018-2024 The Carpentries

https://github.com/TUDelft-MUDE/source-files/raw/main/file/mconflict3.png

Fig. 2.10 Merge conflict#

In this situatie, it is attempted to merge main in a conflicting branch. The conflicting lines on the main branches are preceded by <<<<<<< main, while the changes from the conflicted main branch are preceded by ======= and followed by >>>>>>> conflicting-instructions. To fix this conflict, we need to open the file in an editor and resolve the conflict by keeping the desired changes and removing the unnecessary parts. Once done, we can commit the resolved changes. Note that the current branch is now in a “MERGING” state. We have decided to take the best out of the two branches and merge their changes.

Why merge into the conflicting branch first?#

Merging the main branch into the conflicting branch first allows you to resolve conflicts in a controlled environment. After that you can thoroughly test the merged changes in the conflicting branch before integrating them into the main branch, reducing the risk of introducing bugs.

Exercise

Given is the following file poem.md which is stored in the ‘main’ branch of your repository:

Roses are red,
Violets are blue,
Sugar is sweet,
And so are you.

You and your friend want to improve this poem.

You edit the file in your new branch ‘Q2’:

Roses are red,
Violets are blue,
Sugar is sweet,
Glad I made Q1 through.

Your friend edits the file in his branch ‘love’:

Roses are red,
Violets are blue,
Sugar is sweet,
And my love for MUDE is true.

What would happen when you merge the branch ‘love’ from your friend?

The fourth line in the ‘main’ branch of your repository will be replaced with the new line from your friend

Show answer(s)

Try again

What would happen when you merge your branch ‘Q2’ into ‘main’ after merging the branch ‘love’ from your friend? Why does that happen? How do you solve that?

A merge conflict arises because now there are two conflicting changes for the fourth line of the poem. Git will not know which change to keep and will mark the conflict in the file.

To fix this conflict, you will have to open the file and manually edit the fourth line to keep the change you want.

Show answer(s)

Try again

As instead of merging ‘Q2’ into ‘main’, after merging the branch ‘love’ from your friend, you first merge ‘main’ into ‘Q2’, what would happen when you merge ‘Q2’ into main? How would that be different from the previous question?

You will still have a merge conflict, but now you will have already resolved the conflict in ‘Q2’. If this would have been code, you could have tested the resolved conflict. Therefore, when you merge ‘Q2’ into ‘main’, you can be confident that the changes are correct and won’t introduce bugs.

Show answer(s)

Try again

Collaborating with branches#

When working together on a single branch (so in the same version), it is important to coordinate with your team to avoid conflicts and ensure smooth collaboration. Here are some best practices:

  • Keep in touch with your team about who is working on what.

  • Frequently pull the latest changes to keep your local branch up to date. This helps in minimizing conflicts.

  • Make small, frequent commits with clear messages. This makes it easier to track changes and resolve conflicts.

  • If conflicts arise, communicate with your team directly to resolve them before proceeding.

Why notebooks causes conflicts all the time

Yeah, this is really annoying. The JSON structure of a notebook causes the issue, as explained here.

Attribution

This chapter reuses material from Learn Programming for Engineers and was written by Kiril Vasilev, Riccardo Taormina, Robert Lanzafame, Tom van Woudenberg. Find out more here