Bird
Raised Fist0
Gitdevops~10 mins

Resolving merge conflicts in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Resolving merge conflicts
Start merge
Git detects conflict?
NoMerge completes
Yes
Show conflict markers in files
User edits files to resolve conflicts
User marks conflicts resolved (git add)
User completes merge (git commit)
Merge finished successfully
This flow shows how Git detects conflicts during a merge, how the user resolves them by editing files, and then completes the merge.
Execution Sample
Git
git merge feature-branch
# Conflict detected in file.txt
# Edit file.txt to fix conflicts
git add file.txt
git commit -m "Resolve merge conflict"
This sequence merges a branch, resolves conflicts in a file, stages the fix, and commits the merge.
Process Table
StepActionGit StateUser ActionResult
1Run 'git merge feature-branch'Merge started, conflict detected in file.txtNoneMerge paused, conflict markers added in file.txt
2Open file.txtConflict markers visibleEdit file.txt to choose correct codeConflicts resolved in file.txt content
3Run 'git add file.txt'Conflict marked as resolvedStage resolved filefile.txt ready for commit
4Run 'git commit -m "Resolve merge conflict"'Merge commit createdComplete merge with commitMerge finished successfully
💡 Merge completes after user resolves conflicts and commits
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
file.txtClean versionContains conflict markers <<<<<<< >>>>>>>Edited to resolved contentStaged resolved contentCommitted resolved content
Key Moments - 3 Insights
Why does Git add conflict markers in the file?
Git inserts conflict markers to show the conflicting parts from each branch, as seen in Step 1 and Step 2 of the execution_table.
What does 'git add' do after resolving conflicts?
'git add' tells Git the conflicts in the file are resolved and stages the file for commit, shown in Step 3.
Why must you commit after resolving conflicts?
Committing finalizes the merge and records the resolution, as shown in Step 4 where the merge finishes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Git state immediately after running 'git merge feature-branch'?
ANo conflicts detected
BMerge completed successfully
CMerge started, conflict detected in file.txt
Dfile.txt staged for commit
💡 Hint
Check Step 1 in the execution_table under 'Git State'
At which step does the user mark the conflict as resolved by staging the file?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 'git add file.txt' in the execution_table
If the user forgets to run 'git add' after editing the file, what will happen?
AGit will keep the merge paused, waiting for conflict resolution
BMerge will complete automatically
CGit will still commit the merge
DGit will delete the conflicting file
💡 Hint
Refer to the role of 'git add' in Step 3 of the execution_table
Concept Snapshot
git merge may detect conflicts if changes overlap
Git adds conflict markers in files to show conflicts
User edits files to resolve conflicts manually
Run 'git add <file>' to mark conflicts resolved
Run 'git commit' to complete the merge
Full Transcript
When you run 'git merge' and Git finds conflicting changes, it pauses the merge and adds conflict markers in the affected files. You open those files and edit them to fix the conflicts by choosing or combining changes. After editing, you run 'git add' on the files to tell Git the conflicts are resolved. Finally, you run 'git commit' to finish the merge and save the resolution. This process ensures you control how conflicting changes are combined.

Practice

(1/5)
1. What does a merge conflict in Git mean?
easy
A. Git found changes in the same file that it cannot combine automatically.
B. Git has successfully merged all changes without any issues.
C. Git deleted a file during the merge process.
D. Git created a new branch automatically.

Solution

  1. Step 1: Understand merge conflict meaning

    A merge conflict happens when Git sees changes in the same part of a file from different branches and cannot decide which to keep.
  2. Step 2: Identify what Git does in this case

    Git stops the merge and marks the conflict in the file for you to fix manually.
  3. Final Answer:

    Git found changes in the same file that it cannot combine automatically. -> Option A
  4. Quick Check:

    Merge conflict = conflicting changes in same file [OK]
Hint: Merge conflict means manual fix needed for overlapping changes [OK]
Common Mistakes:
  • Thinking Git merges all changes automatically
  • Confusing conflict with branch creation
  • Assuming files are deleted automatically
2. Which Git command is used to mark a conflict as resolved after editing the file?
easy
A. git add <file>
B. git commit -m 'resolve conflict'
C. git merge --continue
D. git checkout --conflict

Solution

  1. Step 1: Identify how to tell Git conflict is fixed

    After editing the conflicted file, you must stage it to tell Git the conflict is resolved.
  2. Step 2: Choose the correct command to stage files

    The command to stage files is git add <file>.
  3. Final Answer:

    git add <file> -> Option A
  4. Quick Check:

    Stage resolved file with git add [OK]
Hint: Use git add to mark conflict resolved before commit [OK]
Common Mistakes:
  • Trying to commit before staging resolved files
  • Using git merge --continue without staging
  • Using invalid commands like git checkout --conflict
3. Given this conflict marker in a file after a merge:
<<<<<<< HEAD
Line A
=======
Line B
>>>>>>> feature-branch
What will the file content be after you keep only the changes from the feature-branch and stage the file?
medium
A. Line A
B. Line B
C. <<<<<<< HEAD Line A ======= Line B >>>>>>> feature-branch
D. Line A Line B

Solution

  1. Step 1: Understand conflict markers

    The lines between <<<<<<< HEAD and ======= are from current branch; lines between ======= and >>>>>>> feature-branch are from the other branch.
  2. Step 2: Keep only feature-branch changes

    To keep only feature-branch changes, remove the markers and the HEAD section, leaving just 'Line B'.
  3. Final Answer:

    Line B -> Option B
  4. Quick Check:

    Keep feature-branch changes = Line B [OK]
Hint: Remove conflict markers, keep desired lines, then git add [OK]
Common Mistakes:
  • Leaving conflict markers in file
  • Keeping both changes without cleaning markers
  • Confusing which side is which branch
4. You tried to merge a branch but Git reports conflicts. You edited the files but forgot to stage them before committing. What happens if you run git commit now?
medium
A. Git commits the merge with unresolved conflicts included.
B. Git automatically stages and commits the resolved files.
C. Git refuses to commit and shows an error about unmerged paths.
D. Git aborts the merge and resets to previous state.

Solution

  1. Step 1: Understand commit behavior during merge conflicts

    Git requires you to stage resolved files before committing a merge. If files are not staged, Git sees conflicts as unresolved.
  2. Step 2: What happens when committing without staging

    Git will refuse to commit and show an error about unmerged paths, preventing incomplete merges.
  3. Final Answer:

    Git refuses to commit and shows an error about unmerged paths. -> Option C
  4. Quick Check:

    Commit without staging resolved files = error [OK]
Hint: Always git add resolved files before git commit [OK]
Common Mistakes:
  • Assuming git commit auto-stages files
  • Thinking Git commits partial merges
  • Expecting merge to abort automatically
5. You have a file with multiple merge conflicts from two branches. You want to keep all changes from both branches but Git shows conflicts. What is the best way to resolve this?
hard
A. Run git reset --hard to discard all changes and merge again.
B. Use git merge --abort to cancel the merge and try again.
C. Delete the file and create a new one with combined content.
D. Manually edit the file to combine both changes, remove conflict markers, then stage and commit.

Solution

  1. Step 1: Understand the goal to keep all changes

    Since you want to keep both branches' changes, you must manually combine them in the file.
  2. Step 2: How to resolve conflicts properly

    Edit the file to merge both changes, remove conflict markers, then stage with git add and commit the merge.
  3. Final Answer:

    Manually edit the file to combine both changes, remove conflict markers, then stage and commit. -> Option D
  4. Quick Check:

    Manual merge + stage + commit = keep all changes [OK]
Hint: Edit conflicts to combine changes, then git add and commit [OK]
Common Mistakes:
  • Aborting merge instead of resolving
  • Deleting files losing changes
  • Resetting hard losing all work