Merge Strategis and Conflicts
Understanding Merge Strategies
What is Merging? Real-Life Analogy
Think of merging like combining two recipe improvements:
Imagine you and your roommate both improve the same chocolate chip cookie recipe:
- You added vanilla extract and reduced sugar
- Your roommate added nuts and increased baking time
- Now you want to combine both improvements into one perfect recipe
This is exactly what Git merging does with code files!
The Two Main Merge Strategies
Fast-Forward Merge (The Simple Case)
Real-Life Analogy: Your Study Notes Timeline
Imagine you're working on study notes:
- Monday: You write Chapter 1 notes
- Tuesday: You add Chapter 2 notes
- Wednesday: You add Chapter 3 notes
This is a straight line of changes - no conflicts, just adding new content.
Command Line Example:
# Your current work
git log --oneline
# Output shows:
# abc123 Add Chapter 3 notes
# def456 Add Chapter 2 notes
# ghi789 Add Chapter 1 notes
# Someone else adds Chapter 4, you want to merge it
git merge feature-chapter4
# Result: "Fast-forward" - just adds their work to your timeline
GitHub Website Way:
- Go to your repository on GitHub
- Click "Pull requests" tab
- Click "New pull request"
- Select branches to compare
- If GitHub shows "Able to merge" with a green checkmark
- Click "Create pull request" → "Merge pull request"
- Choose "Create a merge commit" or "Squash and merge"
When This Happens:
- Your changes don't overlap with theirs
- Like adding new pages to a book - no conflicts!
Three-Way Merge (The Common Case)
Real-Life Analogy: Group Project Timeline
Imagine a group presentation project:
- You worked on slides 1-3 (Introduction)
- Teammate A worked on slides 4-6 (Main Content)
- Teammate B worked on slides 7-9 (Conclusion)
- Now you need to combine all three parts
Visual Example:
Your work: Slide 1 → Slide 2 → Slide 3
↘
MERGE POINT
↗
Teammate's: Slide 4 → Slide 5 → Slide 6
Command Line Example:
# You're on your branch with slides 1-3
git checkout main
git merge teammate-content
# Git automatically creates a merge commit combining both
GitHub Website Way:
- Create Pull Request (same as above)
- GitHub shows "This branch has no conflicts with the base branch"
- Click "Merge pull request"
- Git automatically combines your changes
- Result: One timeline with everyone's contributions
When to Use Each Strategy:
| Strategy | When to Use | Real-Life Example |
|---|---|---|
| Fast-Forward | Simple, linear changes | Adding new chapters to a book |
| Three-Way Merge | Parallel work, may have conflicts | Group project with different sections |
These two strategies handle 99% of your collaboration needs!
Understanding and Resolving Merge Conflicts (45 minutes)
What Are Merge Conflicts?
Real-Life Analogy: Two Editors, Same Paragraph
Imagine you and your study partner both edit the same paragraph in your shared notes:
Original: "The exam is on Friday."
Your version: "The midterm exam is on Friday at 2 PM." Partner's version: "The final exam is on Friday in Room 101."
The Conflict: Which version is correct? Git can't decide automatically!
How Conflicts Look in Files
When Git finds a conflict, it marks the file like this:
The exam is on Friday.
<<<<<<< HEAD (Your changes)
The midterm exam is on Friday at 2 PM.
=======
The final exam is on Friday in Room 101.
>>>>>>> partner-branch (Their changes)
Study topics include math and science.
Breaking Down the Conflict Markers:
<<<<<<< HEAD= "Here's your version"======== "Here's the dividing line">>>>>>> partner-branch= "Here's their version"
Method 1: Resolving Conflicts via Command Line
Step-by-Step Process:
Step 1: Identify the Conflict
git merge partner-branch
# Output: "Automatic merge failed; fix conflicts and then commit the result."
git status
# Shows: "You have unmerged paths"
# Lists conflicted files in red
Step 2: Open the Conflicted File
# Open in your text editor
code conflicted-file.txt
# or
nano conflicted-file.txt
Step 3: Choose Your Resolution
You have three choices:
- Keep only your version (delete their lines)
- Keep only their version (delete your lines)
- Combine both versions (merge the best of both)
Example Resolution (Combining Both):
The exam is on Friday.
The midterm AND final exams are on Friday. Midterm at 2 PM, Final in Room 101.
Study topics include math and science.
Step 4: Clean Up and Commit
# Remove all conflict markers (<<<, ===, >>>)
# Save the file
# Tell Git the conflict is resolved
git add conflicted-file.txt
# Complete the merge
git commit -m "Merge partner-branch: combine exam information"
Method 2: Resolving Conflicts via GitHub Website
GitHub's Visual Conflict Resolution:
Step-by-Step Process:
Step 1: Start the Pull Request
- Go to your repository on GitHub
- Click "Pull requests" → "New pull request"
- Select your branch to merge
- Click "Create pull request"
Step 2: GitHub Detects Conflict
- Red X appears with message "This branch has conflicts that must be resolved"
- GitHub shows "Resolve conflicts" button
Step 3: Use GitHub's Visual Editor
- Click "Resolve conflicts" button
- GitHub opens visual editor showing:
- Your changes highlighted in green
- Their changes highlighted in red
- Conflict markers clearly visible
Step 4: Make Your Choice
- Edit directly in GitHub's editor
- Delete unwanted lines
- Remove conflict markers (
<<<,===,>>>) - Click "Mark as resolved"
Step 5: Complete the Merge
- Click "Commit merge"
- Add descriptive message
- Click "Merge pull request"
Example - Student Group Project:
Scenario: Two students editing the same conclusion paragraph.
GitHub Visual Conflict Resolution:
Original: "In conclusion, this project was educational."
<<<<<<< your-branch (highlighted in green)
In conclusion, this project was educational and helped us learn teamwork.
=======
In conclusion, this project was very educational and taught us valuable skills.
>>>>>>> teammate-branch (highlighted in red)
Your Resolution Choice:
In conclusion, this project was very educational, helped us learn teamwork, and taught us valuable skills.
Common Conflict Scenarios for Students
Scenario 1: Bibliography Conflicts
Problem: Both students add different sources to the same bibliography section.
Your addition:
Sources:
1. Smith, J. (2023). Introduction to Biology
Teammate's addition:
Sources:
1. Jones, K. (2023). Advanced Chemistry
Smart Resolution:
Sources:
1. Jones, K. (2023). Advanced Chemistry
2. Smith, J. (2023). Introduction to Biology
Scenario 2: Editing Same Sentence
Problem: Both improve the same sentence differently.
Original: "This method is good." Your version: "This method is effective and reliable." Their version: "This method is good for beginners."
Smart Resolution: "This method is effective, reliable, and good for beginners."
Git Bisect - Finding Problematic Changes
What is Git Bisect?
Real-Life Analogy: Finding When Your Plant Started Dying
Imagine your plant was healthy a month ago but is dying now:
- Method 1: Check every single day (slow!)
- Method 2: Use "binary search" - check middle point first
Git Bisect Logic:
- You know the bug wasn't there 10 commits ago
- You know the bug exists now
- Git bisect finds the exact commit that introduced the bug
How Git Bisect Works - Binary Search
Visual Example:
Commits: A → B → C → D → E → F → G → H (current)
Status: ✅ ? ? ? ? ? ? ❌
Step 1: Test middle (D)
If D is good: A → B → C → D → E → F → G → H
✅ ? ? ? ❌
Step 2: Test middle of E-H (F)
If F is bad: A → B → C → D → E → F → G → H
✅ ? ❌ ? ❌
Step 3: Test E
Result: E introduced the bug!
Command Line Git Bisect
Step-by-Step Process:
Step 1: Start Bisecting
# Start the bisect process
git bisect start
# Mark current commit as bad (has the bug)
git bisect bad
# Mark a known good commit (use commit hash or HEAD~10 for "10 commits ago")
git bisect good HEAD~10
Step 2: Git Automatically Checks Out Middle Commit
# Git says: "Bisecting: 5 revisions left to test after this"
# Git automatically switches to the middle commit
# Test your application/file
# Is the bug present?
Step 3: Tell Git Your Results
# If bug exists in this commit:
git bisect bad
# If bug doesn't exist in this commit:
git bisect good
# Git automatically moves to next commit to test
Step 4: Repeat Until Found
# Keep testing and marking as good/bad
# Git narrows down the range each time
# When Git finds the problematic commit:
# "abc123 is the first bad commit"
Step 5: End Bisect Session
# Return to your original branch
git bisect reset
Practical Student Example - Assignment Submission
Scenario: Your assignment worked fine last week, but now it has errors.
Real-World Bisect Process:
Step 1: Setup
# Start bisecting
git bisect start
# Current version has errors
git bisect bad
# Version from last week was fine
git bisect good HEAD~7 # 7 commits ago (roughly a week)
Step 2: Test Each Version
# Git checks out commit from middle of week
# Test: "Does my assignment run without errors?"
# If errors exist:
git bisect bad
# If no errors:
git bisect good
# Repeat until Git finds the exact commit that broke it
Step 3: Analyze the Result
# Git shows: "commit def456 is the first bad commit"
# Look at what changed in that commit:
git show def456
GitHub Website Alternative (Limited)
GitHub Limitation: You cannot run git bisect directly on GitHub website.
However, you can:
- Use GitHub's commit history to manually binary search
- Click "Commits" to see timeline
- Click on commits from different time periods
- Download or view files at different points
- Manually identify when problem started
GitHub-Assisted Bisect Process:
- Go to repository → "Commits"
- Identify time range (e.g., "worked 2 weeks ago, broken now")
- Click commit from 1 week ago - download and test
- If that's good, try commits from this week
- If that's bad, try commits from 10 days ago
- Narrow down until you find the problematic commit
Team Collaboration Tips
For Group Projects:
- Communicate changes before merging
- Use descriptive commit messages
- Pull before push - get updates first
- Resolve conflicts together when possible
- Test the merged result as a team
Division of Labor:
- Designate one "merge master" for complex projects
- Let experienced member handle difficult conflicts
- Everyone should know basic conflict resolution
- Use GitHub website for transparent process
Essential Commands:
# Merging
git merge branch-name # Merge another branch
git merge --abort # Cancel merge if problems
# Conflict Resolution
git status # See conflicted files
git add resolved-file # Mark conflict as resolved
git commit # Complete the merge
# Bisecting
git bisect start # Begin bisect session
git bisect good/bad # Mark commits
git bisect reset # End bisect session
GitHub Website Actions:
- Pull Request → Merge pull request (for merging)
- Resolve conflicts button (for conflicts)
- Commits page (for manual bisect)