Lesson 3 โข Intermediate
Branching and Merging ๐ฟ
Create isolated branches to build features, merge them back safely, and learn when to rebase vs merge โ the skill that unlocks team collaboration.
What You'll Learn in This Lesson
- โข Creating, switching, and deleting branches
- โข Fast-forward vs three-way merges
- โข Rebase for clean, linear history
- โข
git stashโ save work temporarily - โข The golden rule of rebasing
git init test-branching to practice safely.1๏ธโฃ Branch Basics
A branch is just a pointer to a commit. Creating a branch is nearly instant โ Git doesn't copy files, it just creates a new pointer. This makes branching incredibly lightweight.
Try It: Branch Basics
Create, list, and switch between branches
// Branches โ Work in Isolation
console.log("=== What Are Branches? ===");
console.log("A branch is an independent line of development.");
console.log("The default branch is called 'main' (or 'master').");
console.log();
console.log("=== Why Branch? ===");
console.log("โข Work on a feature without breaking main");
console.log("โข Multiple people work on different things simultaneously");
console.log("โข Experiment safely โ delete the branch if it fails");
console.log("โข Keep main always deployable
...2๏ธโฃ Merging Branches
Merging brings changes from one branch into another. Fast-forward merges simply move the pointer. Three-way merges create a merge commit when both branches have diverged.
Try It: Merging
Fast-forward and three-way merges visualized
// Merging โ Combining Branches
console.log("=== git merge โ Bring Changes Together ===");
console.log();
console.log("Steps to merge a feature branch into main:");
console.log(" 1. $ git checkout main โ Switch to target");
console.log(" 2. $ git merge feature-login โ Merge feature into main");
console.log();
console.log("=== Fast-Forward Merge ===");
console.log("When main hasn't changed since the branch was created:");
console.log();
console.log(" Before: main: A โโ B");
conso
...3๏ธโฃ Rebase vs Merge
Rebase replays commits on top of another branch for a clean, linear history. It's powerful but dangerous if used on shared branches. Follow the golden rule: never rebase pushed commits.
Try It: Rebase
Understand when to rebase and when to merge
// Rebase vs Merge
console.log("=== git rebase โ Linear History ===");
console.log();
console.log("Rebase replays your commits on top of another branch,");
console.log("creating a clean, linear history.");
console.log();
console.log("=== Before Rebase ===");
console.log(" main: A โโ B โโ E โโ F");
console.log(" \\");
console.log(" feat: C โโ D");
console.log();
console.log("=== After: git rebase main (on feat branch) ===");
console.log(" main: A โโ B โโ E โโ F");
conso
...4๏ธโฃ git stash โ Quick Save
git stash temporarily shelves your changes so you can switch branches. When you come back, git stash pop restores everything exactly as it was.
Try It: git stash
Save work temporarily and restore it later
// git stash โ Save Work Temporarily
console.log("=== git stash โ Quick Save ===");
console.log("Stash saves your uncommitted changes and cleans your working directory.");
console.log("Perfect when you need to switch branches mid-work.");
console.log();
console.log("=== Scenario ===");
console.log("You're working on feature-login but need to fix a bug on main:");
console.log();
console.log(" 1. $ git stash โ Save current changes");
console.log(" Saved working directory and ind
...โ ๏ธ Common Mistakes
git branch -d name after merging.git log --oneline --graph --all to visualize your entire branch structure in the terminal.๐ Quick Reference
| Command | Purpose |
|---|---|
| git branch name | Create a branch |
| git switch -c name | Create and switch |
| git merge branch | Merge branch into current |
| git rebase main | Replay on top of main |
| git stash / pop | Save/restore work |
| git branch -d name | Delete merged branch |
๐ Lesson Complete!
You now understand branching, merging, and rebasing! Next, we'll push your code to GitHub and learn remote repository workflows.
Sign up for free to track which lessons you've completed and get learning reminders.