Computer Science
Grade 9
20 min
8. Resolving Merge Conflicts
Introduce the concept of merge conflicts and how to resolve them.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Identify the terminal output that signals a merge conflict.
Explain why a merge conflict occurs using the concept of parallel edits.
Interpret Git's conflict markers (<<<<<<<, =======, >>>>>>>) to locate conflicting code sections.
Manually edit a file to resolve a conflict by choosing, combining, or rewriting code.
Use `git add` and `git commit` to finalize a merge after resolving a conflict.
Differentiate between the 'ours' (HEAD) and 'theirs' (incoming) changes within a conflict.
Ever tried to edit the same sentence in a group project document at the exact same time as a friend? 💥 Git has a similar problem, and we're going to learn how to be the expert who fixes it!
This lesson te...
2
Key Concepts & Vocabulary
TermDefinitionExample
Merge ConflictAn event that occurs when Git is unable to automatically merge changes from two different branches because they both modified the same part of the same file.You change the title on line 1 of `index.html` to 'My Awesome Site' on the `main` branch. Your friend, on a `feature` branch, changes the same line to 'Our Cool Project'. When you try to merge `feature` into `main`, Git doesn't know which title to keep, causing a conflict.
Conflict MarkersSpecial lines of text (<<<<<<<, =======, >>>>>>>) that Git automatically inserts into a conflicted file to show you exactly where the conflicting changes are and which branch they came from.Git will change the file to look like this:
<<<<...
3
Core Syntax & Patterns
The Conflict Marker Structure
<<<<<<< HEAD
[Code from your current branch ('ours')]
=======
[Code from the other branch ('theirs')]
>>>>>>> [name-of-other-branch]
This is the pattern Git uses to show you a conflict. You must manually delete all three marker lines (<<<<<<<, =======, >>>>>>>) and edit the code in between to create the final version you want to keep.
The Conflict Resolution Workflow
1. `git merge <branch-name>` -> 2. See CONFLICT message -> 3. Open conflicted file(s) -> 4. Edit file(s) to fix -> 5. `git add <resolved-file(s)>` -> 6. `git commit`
This is the required sequence of commands to resolve a merge conflict. After you ma...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A developer starts a merge, gets a conflict, and then leaves for the day without resolving it. When they return and run `git status`, what is the most likely output they will see, and what does it imply?
A.It will show 'nothing to commit, working tree clean' and imply the merge was automatically resolved.
B.It will show 'unmerged paths' and a message that they are in the middle of a merge, implying they must resolve, add, and commit to finish.
C.It will show an error message and imply that the repository is corrupted.
D.It will show the changes as 'staged for commit' and imply they only need to run `git commit`.
Challenging
Your teammate is merging your `bug-fix` branch into `main`. A conflict occurs. Your change in `bug-fix` was essential to prevent the app from crashing. Your teammate resolves the conflict by choosing their version from `main` ('ours') without consulting you, which re-introduces the crash. Which 'Common Pitfall' does this scenario best illustrate?
A.Committing the Conflict Markers
B.Forgetting to `git add`
C.Choosing 'Ours' or 'Theirs' Without Understanding
D.Editing Files Outside the Conflict Markers
Challenging
The conflict resolution workflow is `merge` -> `edit file` -> `git add` -> `git commit`. Why is the `git add` step so fundamentally important in this specific workflow, and what unique information does it signal to Git?
A.It signals that you want to delete the file.
B.It's the same as a normal `git add`; it just adds new content to the staging area.
C.It specifically signals to Git that the human developer has resolved the conflict for that file, transitioning it from an 'unmerged' to a 'resolved' state.
D.It signals that you want to undo the merge and go back to the previous state.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free