Module Overview
What You'll Learn
- Create and manage branches
- Merge branches safely
- Resolve merge conflicts
- Use branching strategies
Time Estimate
40-55 minutes
Prerequisites
Module 2: Working with Files & History
Understanding Branches
What are Branches?
Branches are lightweight pointers to commits that allow you to work on different features or versions of your code simultaneously.
Key Concepts
- Main/Master: The default branch (usually contains production-ready code)
- Feature Branches: Separate branches for new features or bug fixes
- HEAD: Points to the current branch you're working on
- Branch Pointer: Moves forward as you make commits
Branch Management Commands
View branches
git branch
Create a new branch
git branch feature-name
Switch to a branch
git checkout feature-name
Create and switch in one command
git checkout -b feature-name
Delete a branch
git branch -d feature-name
🌿 Practice Branching
Try these branch operations:
Merging Branches
Types of Merges
Fast-Forward Merge
When the target branch hasn't diverged from the source branch
git merge feature-branch
Three-Way Merge
When both branches have new commits since they diverged
git merge feature-branch
Merge Process
1. Switch to target branch
git checkout main
2. Merge the feature branch
git merge feature-branch
3. Resolve conflicts (if any)
# Edit conflicted files, then:
git add resolved-file.txt
4. Complete the merge
git commit
🔄 Practice Merging
Try this merge workflow:
Resolving Merge Conflicts
What Causes Conflicts?
Conflicts occur when Git can't automatically merge changes because the same lines have been modified in different ways in both branches.
Common Conflict Scenarios
- • Same file modified in both branches
- • File deleted in one branch, modified in another
- • File renamed in one branch, modified in another
Conflict Resolution Process
1. Identify conflicted files
git status
2. Open conflicted files
Look for conflict markers:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> feature-branch
3. Edit the file to resolve conflicts
Remove conflict markers and choose which changes to keep
4. Stage the resolved file
git add resolved-file.txt
5. Complete the merge
git commit
⚠️ Practice Conflict Resolution
Simulate a merge conflict:
Branching Strategies
Git Flow
Main Branches
- main: Production-ready code
- develop: Integration branch
Supporting Branches
- feature/*: New features
- release/*: Release preparation
- hotfix/*: Production fixes
GitHub Flow (Simplified)
Create a branch
From main for each new feature or fix
Make commits
Add and commit your changes
Push to GitHub
Share your branch with the team
Create Pull Request
Request review and merge
Merge to main
After review and approval
Module Quiz
Test your understanding of branching and merging: