Branches - Parallel Universes

Branches

Scenario: You're mid-way through a risky new feature when an urgent bug needs fixing on the live site. You don't want your half-done feature going out. Branches solve exactly this: work on the feature in isolation, switch away to fix the bug, and come back.

A branch is an independent line of commits. The default is usually main.

CommandDoes
git branchList branches (* marks current)
git branch featureCreate a branch
git switch featureSwitch to it (modern)
git switch -c featureCreate and switch
git checkout featureSwitch to it (older syntax)
git branch -d featureDelete a merged branch
$ git switch -c feature-login
Switched to a new branch 'feature-login'
$ git branch
* feature-login
  main
Analogy: Branching is a parallel universe for your code. Commits you make on feature-login don't touch main at all until you decide to merge them back.
Tip: git switch (create/change branches) and git restore (discard file changes) are the modern replacements for the overloaded git checkout, which did both and confused everyone.