Module 6: Branches & Merging (Hands-on!)¶
๐ฏ What you'll learn: How to create parallel timelines for experimentation, work on features without breaking the main project, and merge changes back โ the most powerful concept in Git.
๐ง Hands-on module! You'll create branches, switch between them, and merge โ in both CLI and GitHub Desktop.
Why Branches?¶
Imagine your Cloud Cafรฉ is doing well. The main menu works great. Now you want to:
- โ Try a new "Seasonal Specials" section
- ๐จ Redesign the menu layout
- ๐ฐ Test new prices
Problem: If you experiment directly on the menu and it goes wrong โ customers see a broken menu!
Solution: Make a copy where you can experiment safely. If it works, add it to the real menu. If it fails, throw it away. The real menu was never affected.
That's a branch.
The Analogy โ Restaurant Kitchen ๐ณ¶
Main Menu (master/main branch) Experimental Kitchen (feature branch)
โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ Flat White $5.00 โ โ โ Flat White $5.00 โ
โ โ Long Black $4.50 โ โ โ Long Black $4.50 โ
โ โ Cappuccino $5.50 โ โ โ Cappuccino $5.50 โ
โ โ Mocha $6.00 โ โ โ Mocha $6.00 โ
โ โ โ ๐ Pumpkin Latte $7.00 โ โ testing!
โ โ โ ๐ Spiced Chai $6.50 โ โ testing!
โ Customers see THIS โ โ Only you see this โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
If it works:
MERGE into main menu โ
If it fails:
Delete the branch โ
Main menu was never affected!
What is a Branch?¶
A branch is a separate line of development. It starts from a point in your history and lets you make changes without affecting the main line.
gitGraph
commit id: "Initial menu"
commit id: "Added Mocha"
commit id: "Added hours"
branch seasonal-specials
commit id: "Added Pumpkin Latte"
commit id: "Added Spiced Chai"
checkout main
commit id: "Fixed typo"
merge seasonal-specials id: "Merge specials"
commit id: "Updated prices"
Key Terms¶
| Term | What It Means | Analogy |
|---|---|---|
| main (or master) | The primary branch โ your "real" menu | The published restaurant menu |
| feature branch | A temporary branch for experimenting | The test kitchen |
| checkout / switch | Move to a different branch | Walk to a different kitchen |
| merge | Combine a branch back into main | Add tested dishes to the real menu |
| HEAD | Which branch you're currently on | Which kitchen you're standing in |
Branch Commands¶
| Command | What It Does |
|---|---|
git branch |
List all branches (current one has *) |
git branch seasonal-specials |
Create a new branch |
git switch seasonal-specials |
Move to that branch |
git switch -c new-branch |
Create AND switch in one command |
git switch master |
Go back to master/main |
git merge seasonal-specials |
Merge a branch into current branch |
git branch -d seasonal-specials |
Delete a branch (after merging) |
Hands-on: Create a Branch, Make Changes, Merge¶
Step 1: Go to your cloud-cafe repo¶
Step 2: Create and switch to a new branch¶
This creates a branch called seasonal-specials and moves you to it. Check:
You should see:
Step 3: Make changes on the branch¶
Add seasonal items to menu.md:
"`n## Seasonal Specials`n- Pumpkin Latte - `$7.00`n- Spiced Chai - `$6.50`n- Hot Chocolate - `$5.50" | Add-Content menu.md
Stage and commit:
Step 4: Switch back to master¶
Now open menu.md โ the seasonal specials are GONE! They only exist on the seasonal-specials branch.
Switch back:
Open menu.md โ they're BACK! Each branch is its own timeline.
Step 5: Merge the branch into master¶
When you're happy with the seasonal specials, merge them into the main menu:
Now menu.md on master has the seasonal specials! Check with:
Step 6: Delete the branch (cleanup)¶
The branch has served its purpose โ clean up:
What Happens During a Merge?¶
When Git merges, it takes the changes from one branch and combines them into another:
Before merge:
master: 1 โโ 2 โโ 3 โโ 5 (fixed typo)
\
seasonal-specials: 4 โโ 6 (seasonal items)
After merge (git merge seasonal-specials while on master):
master: 1 โโ 2 โโ 3 โโ 5 โโ 7 (merge commit)
\ /
seasonal-specials: 4 โโ 6 โโโโโโ
Git is smart โ it combines the changes automatically, as long as they don't conflict.
Merge Conflicts โ When Two People Edit the Same Line¶
What is a conflict? It happens when two branches change the same line in the same file. Git doesn't know which version to keep, so it asks YOU to decide.
Example¶
On master, you changed the Flat White price to $5.50.
On seasonal-specials, you changed the Flat White price to $4.50.
When you merge, Git says: "Both branches changed line 4. I don't know which price is correct. You decide."
The file will look like this:
## Hot Drinks
<<<<<<< HEAD
- Flat White - $5.50
=======
- Flat White - $4.50
>>>>>>> seasonal-specials
| Marker | Meaning |
|---|---|
<<<<<<< HEAD |
Start of YOUR branch's version (master) |
======= |
Separator between the two versions |
>>>>>>> seasonal-specials |
End of the other branch's version |
How to Resolve¶
- Open the file
- Delete the markers (
<<<<,====,>>>>) - Keep the version you want (or combine both)
- Save the file
git addandgit commit
Conflicts are normal
Don't panic when you see a conflict! It just means Git needs your help making a decision. In practice, conflicts are rare if people work on different files.
Branches in GitHub Desktop¶
The same operations in GitHub Desktop:
| CLI Command | GitHub Desktop |
|---|---|
git branch |
Branch dropdown (top bar) |
git switch -c new-branch |
Branch โ New Branch |
git switch master |
Click the branch name in dropdown |
git merge |
Branch โ Merge into Current Branch |
git branch -d |
Branch โ Delete |
GitHub Desktop also has a visual branch selector showing where you are.
โ Module 6 Summary¶
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Module 6 Recap โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Branch = parallel timeline for safe experimenting โ
โ master/main = the "real" version โ
โ โ
โ git switch -c <name> โ Create + switch to branch โ
โ git switch master โ Go back to main โ
โ git merge <branch> โ Bring changes into current โ
โ git branch -d <name> โ Delete after merging โ
โ โ
โ Conflicts happen when same line changed in both โ
โ branches โ resolve by editing, then add + commit โ
โ โ
โ โก๏ธ Next: Module 7 โ Pull Requests & Collaboration โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐งช Module 6 Quiz¶
Q1: Why would you create a branch instead of working directly on master?
To experiment safely without affecting the working version. If the experiment works, merge it in. If it fails, delete the branch. The main project was never at risk.
Q2: You're on the seasonal-specials branch. You open menu.md and see seasonal items. You switch to master and open menu.md โ the items are gone. Why?
Each branch has its own timeline. The seasonal items only exist in commits on the seasonal-specials branch. Master hasn't received those changes yet. After merging, both branches will have the items.
Q3: What is a merge conflict?
A conflict happens when two branches change the same line in the same file. Git can't decide which version to keep, so it marks the conflict in the file and asks YOU to resolve it by choosing the correct version.
Q4: What command creates a new branch AND switches to it in one step?
git switch -c branch-name โ the -c flag means "create." Without -c, git switch only switches to an existing branch.
Q5: After merging a feature branch, should you keep it or delete it?
Delete it with git branch -d branch-name. The branch has served its purpose โ all its changes are now in master. Keeping old branches around creates clutter. You can always see the merge in the commit history.
โก๏ธ What's Next?¶
In Module 7: Pull Requests & Collaboration, you'll learn the GitHub way of merging โ using Pull Requests to propose, review, and merge changes. This is how teams work together!