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.
| Command | Does |
|---|---|
git branch | List branches (* marks current) |
git branch feature | Create a branch |
git switch feature | Switch to it (modern) |
git switch -c feature | Create and switch |
git checkout feature | Switch to it (older syntax) |
git branch -d feature | Delete 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 onfeature-logindon't touchmainat all until you decide to merge them back.
Tip:git switch(create/change branches) andgit restore(discard file changes) are the modern replacements for the overloadedgit checkout, which did both and confused everyone.