Branching Basics
Branching is Git's most powerful feature. It allows you to create a separate workspace to build features or fix bugs without affecting the stable "Main" project.
1. What is a Branch?
Think of a branch as a lightweight pointer to a specific commit. Creating a branch doesn't copy all your files; it just creates a new pointer that moves forward as you make new commits.
2. Core Branching Commands
The industry is moving from the older checkout command to the modern, more intuitive switch command.
| Action | Classic Command | Modern Command (Recommended) |
|---|---|---|
| List Branches | git branch | git branch |
| Create Branch | git branch [name] | git branch [name] |
| Switch Branch | git checkout [name] | git switch [name] |
| Create & Switch | git checkout -b [name] | git switch -c [name] |
3. Merging: Combining Work
Once your feature is complete, you bring it back to the main path using a Merge.
4. Deleting Branches
To keep your workspace clean, delete branches after they have been merged.
# Safe Delete (Will error if not merged)
git branch -d feature-name
# Force Delete (Risky!)
git branch -D accidental-branch[!TIP] Why use
switch? Thegit checkoutcommand was "overloaded"—it did too many things (restored files, switched branches, created branches). Git introducedswitchandrestoreto make these operations clearer and less prone to accidental errors.