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.
Fig. 2.26 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
Fig. 2.27 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?
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?
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?
Exercise
Imagine the following time-line of event of Krisje and Reinier collaborating with Git to solve a coding challenge:
Krisje creates a git repository with a
README.mdon main and shares it with Reinier.Krisje adds a file
code.pyon main with just two lines of code.Reinier adds three lines of code to
code.pyin a new branch from main with the name update.Reinier opens a pull request for update into main
Krisje adds five lines of code to
code.pyin a new branch from main called feature.Krisje tries to merge feature into update but gets a merge conflict
Krisje solves the merge conflict and commits those changes to update while merging feature into update
Krisje merges update into main
Draw the final git history graph of the repository of Krisje and Reinier
Explain what exactly Krisje might have done to solve the merge conflict in step 7.
How could Krisje and Reinier have avoided the conflict by communicating or working differently? Use coding and git terminology in your answer.
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