# Branching and Merging ## Why branches exist Without branches, every commit happens on the same single line of history — risky work and finished work are indistinguishable, and two people can't develop different things simultaneously without directly interfering with each other. A **branch** is a movable pointer to a commit, letting you develop something in isolation and only combine it back into the main line when it's actually ready. ## Creating and switching branches ``` $ git branch feature-login # create a new branch (doesn't switch to it) $ git checkout feature-login # switch to it $ git switch feature-login # the newer, clearer equivalent of checkout for this purpose $ git checkout -b feature-login # create AND switch in one step ``` Commits made while on `feature-login` only affect that branch — `main` is completely unaffected until you deliberately combine them. ## Merging: combining two branches' history ``` $ git checkout main $ git merge feature-login ``` Git attempts to combine the two branches' history automatically. When the two branches changed DIFFERENT parts of a project, this usually succeeds with no fuss. ## Divergence and conflicts When both branches changed the SAME lines of the SAME file differently, Git can't automatically decide which version is correct — this is a **merge conflict**. Git marks the conflicting section directly in the file: ``` <<<<<<< HEAD your current branch's version ======= the incoming branch's version >>>>>>> feature-login ``` A conflict is not an error you did something wrong — it's Git correctly refusing to silently guess which change you actually want. You resolve it by editing the file to the content you actually want (removing the markers), then staging and committing to complete the merge. ## Instructor Notes **Lesson goal:** Frame merge conflicts as an EXPECTED, normal part of collaborative work — not a sign of failure — before students hit their first real one in this module's Lab. **Approximate timing:** 15 minutes read; 10 minutes live. **Common misconception to watch for:** panicking at the sight of conflict markers — reinforce explicitly that this is Git behaving correctly, not breaking. **Demo idea:** deliberately narrate a conflict as "Git is asking YOU to make a decision it can't make safely on its own" rather than "something went wrong."