Branches and merging

Parallel timelines for trying things without fear.

A branch is a lightweight, movable label on your history. You make one for each idea (a feature, a fix, an experiment), work there, and when it's good you merge it back.

git switch -c feature/dark-mode   # create and move to a new branch
# ...edit files, add, commit...
git switch main                   # go back to main
git merge feature/dark-mode       # bring the work in

Careful: If two branches changed the same lines, git can't decide for you and reports a merge conflict. Open the file, choose what the final text should be, remove the conflict markers, then git add and commit.

Quick check

Why use a branch for a new feature?

  1. It makes the code run faster
  2. You can experiment without touching main until it's ready
  3. Git requires a branch for every commit
  4. It deletes old commits

Answer: You can experiment without touching main until it's ready. Branches isolate your work, so main stays stable while you experiment. Merge when you're happy.