Courses/Git/Branching and Merging

    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

    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

    Try it Yourself ยป
    JavaScript
    // 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

    Try it Yourself ยป
    JavaScript
    // 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

    Try it Yourself ยป
    JavaScript
    // 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

    Try it Yourself ยป
    JavaScript
    // 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

    โš ๏ธ
    Rebasing shared branches โ€” This rewrites history and breaks everyone else's work. Only rebase your own local branches.
    โš ๏ธ
    Not deleting merged branches โ€” Old branches pile up. Delete them with git branch -d name after merging.
    ๐Ÿ’ก
    Pro Tip: Use git log --oneline --graph --all to visualize your entire branch structure in the terminal.

    ๐Ÿ“‹ Quick Reference

    CommandPurpose
    git branch nameCreate a branch
    git switch -c nameCreate and switch
    git merge branchMerge branch into current
    git rebase mainReplay on top of main
    git stash / popSave/restore work
    git branch -d nameDelete 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.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service