Lesson 8: Merging Branches
What You'll Learn
In this lesson, you'll learn how to merge branches - combining work from different branches back together. This is a crucial skill for managing features and collaborating with others.
What is Merging?
Merging combines the commit history from one branch into another. Typically, you:
- Create a feature branch
- Make commits on the feature branch
- When ready, merge the feature branch into main
Real-world analogy:
Imagine writing a report:
- Main branch: The final report
- Feature branch: A draft section you're working on
- Merging: Adding your finished section to the final report
Part 1: Basic Merge Workflow
Step-by-Step Example
# 1. Start on main branch
git switch main
# 2. Create and switch to feature branch
git switch -c feature/add-footer
# 3. Make changes and commit
echo "<footer>© 2026</footer>" >> index.html
git add index.html
git commit -m "Add footer to homepage"
# 4. Switch back to main
git switch main
# 5. Merge the feature branch
git merge feature/add-footer
What happens:
- Git combines commits from both branches
- The footer change is now in the main branch
- Your main branch has the latest code
Part 2: Types of Merges
| Merge Type | When It Happens | Result |
|---|---|---|
| Fast-Forward | No changes on main since branch creation | Git simply moves the main pointer forward |
| Three-Way Merge | Both branches have new commits | Git creates a new "merge commit" |
Fast-Forward Merge
$ git merge feature/new-header
Updating abc1234..def5678
Fast-forward
header.html | 5 +++++
1 file changed, 5 insertions(+)
Fast-forward: Git just moves the main pointer to catch up with the feature branch.
Three-Way Merge
$ git merge feature/new-sidebar
Merge made by the 'recursive' strategy.
sidebar.html | 10 ++++++++++
1 file changed, 10 insertions(+)
Three-way merge: Git creates a new merge commit that combines both branches.
Part 3: After Merging
Delete the Feature Branch
Once merged, the feature branch is no longer needed:
git branch -d feature/add-footer
Push to GitHub
git push origin main
Delete Remote Branch
git push origin --delete feature/add-footer
Part 4: Viewing Merge History
See Graphical Log
git log --oneline --graph --all
Output shows branches and merges visually:
* abc1234 (HEAD -> main) Merge branch 'feature/add-footer'
|\
| * def5678 Add footer to homepage
|/
* ghi9012 Update README
Part 5: Best Practices for Merging
| Practice | Why It Matters |
|---|---|
| Always merge into main from a clean state | No uncommitted changes prevents confusion |
| Pull main before merging | Ensures you have the latest code |
| Test the merged code | Make sure everything works together |
| Delete merged branches | Keeps your branch list clean |
| Write clear merge commit messages | Explains what feature was added |
Part 6: Complete Workflow Example
# 1. Update main branch
git switch main
git pull
# 2. Create feature branch
git switch -c feature/contact-form
# 3. Work on feature (multiple commits)
# ... make changes ...
git add .
git commit -m "Add contact form HTML"
# ... more changes ...
git add .
git commit -m "Style contact form"
# 4. Update from main (good practice)
git switch main
git pull
git switch feature/contact-form
git merge main # Merge main into feature
# 5. Ready to merge - switch to main
git switch main
# 6. Merge feature branch
git merge feature/contact-form
# 7. Push to GitHub
git push
# 8. Clean up
git branch -d feature/contact-form
git push origin --delete feature/contact-form
Practice Exercise
- Create a new branch called
feature/add-styles - Create a file called
style.csswith some CSS - Commit your changes
- Switch back to main
- Merge the feature branch
- Delete the feature branch
- Push to GitHub
git switch -c feature/add-styles
echo "body { margin: 0; }" > style.css
git add style.css
git commit -m "Add basic styles"
git switch main
git merge feature/add-styles
git branch -d feature/add-styles
git push
Summary
Key Commands
| Command | What It Does |
|---|---|
git merge branch-name |
Merge specified branch into current branch |
git branch -d branch-name |
Delete local branch |
git log --graph |
View merge history visually |
What's Next?
In the next lesson, you'll learn how to handle merge conflicts - what happens when Git can't automatically merge changes, and how to resolve them manually.