Books/Git Essentials/Understanding Branches

    Understanding Branches

    Understanding Branches

    Branches are one of Git's most powerful features. They let you work on different versions of your project at the same time without affecting your main code.

    What is a Branch?

    Think of branches like parallel universes for your code:

    main          ──●──●──●──●──────────●── (stable code)
                              \            /
    feature-login              ●──●──●──● (experiment here)
    
    • main is your stable, working code
    • feature-login is a copy where you can experiment freely
    • When your experiment works, you merge it back into main

    Why Use Branches?

    ScenarioWithout BranchesWith Branches
    Try a new featureRisky — might break working codeSafe — main code is untouched
    Work on two featuresMessy — changes get mixed togetherClean — each feature is separate
    AI generates codeHard to undo if it's wrongEasy — just delete the branch
    Collaborate with othersConstant conflictsEach person works on their own branch

    Branches + AI Coding Tools

    Branches are especially useful with AI tools:

    1. Create a branch before asking AI to make big changes
    2. If the AI-generated code works, merge it into main
    3. If it doesn't work, delete the branch — no harm done

    What to ask your AI: "Before we make changes, help me create a new branch called 'feature-[name]' so I can experiment safely."

    Creating a Branch

    # Create a new branch
    git branch feature-login
    
    # Create AND switch to a new branch (most common)
    git checkout -b feature-login

    The -b flag means "create a new branch and switch to it immediately."

    Switching Between Branches

    # Switch to an existing branch
    git checkout main
    
    # Switch to another branch
    git checkout feature-login

    When you switch branches, Git updates all your files to match that branch. It's like teleporting between parallel universes.

    Seeing Your Branches

    # List all local branches (current branch has a * next to it)
    git branch

    Output:

      feature-login
    * main
    

    The * shows you're currently on the main branch.

    A Complete Branch Workflow

    Here's a realistic example:

    # 1. Start on main
    git checkout main
    
    # 2. Create a feature branch
    git checkout -b add-dark-mode
    
    # 3. Make changes, commit as you go
    # ... edit files ...
    git add .
    git commit -m "Add dark mode toggle to navbar"
    
    # ... edit more files ...
    git add .
    git commit -m "Add dark mode styles for all pages"
    
    # 4. Switch back to main when done
    git checkout main
    
    # 5. Merge your feature into main
    git merge add-dark-mode

    Merging Branches

    When your feature is ready, merge it into main:

    # First, switch to the branch you want to merge INTO
    git checkout main
    
    # Then merge your feature branch
    git merge feature-login

    If everything goes smoothly, Git combines the changes automatically.

    Merge Conflicts

    Sometimes Git can't automatically merge because two branches changed the same lines in the same file. This is called a merge conflict.

    When this happens, Git marks the conflicting sections in your file:

    <<<<<<< HEAD
    const color = "blue";
    =======
    const color = "red";
    >>>>>>> feature-login
    

    You need to manually choose which version to keep (or combine them), then remove the conflict markers.

    What to ask your AI: "I have a merge conflict in [filename]. Here's what it looks like: [paste conflict]. Can you help me resolve it?"

    Deleting a Branch

    After merging, you can clean up:

    # Delete a merged branch
    git branch -d feature-login

    If the branch was never merged and you want to discard it:

    # Force delete an unmerged branch (careful — this discards all its work)
    git branch -D experiment-gone-wrong

    Branch Naming Conventions

    Use clear, descriptive names:

    feature/user-authentication    # New feature
    fix/broken-login-button        # Bug fix
    experiment/try-new-api         # Experimentation
    

    Common Branch Commands Reference

    CommandWhat It Does
    git branchList all branches
    git branch nameCreate a new branch
    git checkout -b nameCreate and switch to a new branch
    git checkout nameSwitch to an existing branch
    git merge nameMerge a branch into the current branch
    git branch -d nameDelete a merged branch

    What's Next?

    You know how to work with branches locally. The next tutorial covers remote repositories — pushing your code to GitHub so it's backed up and shareable.

    What to ask your AI: "I want to create a branch, make changes, and merge them back. Can you walk me through the process step by step?"


    🌐 www.genai-mentor.ai