Creating Alternate Realities
Branching lets you diverge from the main timeline to experiment, develop features, or fix bugs—all without affecting the stable code. It's Git's killer feature.
What is a Branch?
A branch is a lightweight movable pointer to a commit. The default branch is main (or master). Creating a branch costs nothing and takes milliseconds.
Why Branch?
- Work on features without breaking main code
- Experiment safely
- Collaborate without conflicts
- Organize work logically
- Test ideas before committing to them
Viewing Branches
git branch # List local branches
git branch -a # List all branches (including remote)
git branch -v # Show last commit on each branch
Creating a Branch
git branch feature-navigation
This creates a new branch but doesn't switch to it.
Switching Branches
git checkout feature-navigation
Or with newer Git:
git switch feature-navigation
Create and Switch in One Command
git checkout -b feature-propulsion
# Or
git switch -c feature-propulsion
Branch Naming Conventions
Common patterns:
feature/add-dark-modebugfix/fix-login-timeouthotfix/critical-security-patchexperiment/try-new-algorithm
Use descriptive names. Avoid spaces and special characters.
Working on a Branch
- Create and switch:
git switch -c my-feature - Make changes and commit normally
- Push branch:
git push -u origin my-feature
These commits only affect your branch, not main.
Deleting Branches
After merging a feature:
git branch -d feature-name # Safe delete (only if merged)
git branch -D feature-name # Force delete
Delete remote branch:
git push origin --delete feature-name
Branch Workflow Example
Typical Feature Development
# Start on main
git switch main
git pull
# Create feature branch
git switch -c feature-sensors
# Work on feature (multiple commits)
git add sensors.js
git commit -m "Add temperature sensor"
git add sensors.js
git commit -m "Add pressure sensor"
# Push feature branch
git push -u origin feature-sensors
# Create pull request on GitHub
# After approval and merge, clean up
git switch main
git pull
git branch -d feature-sensors
Viewing Branch History
git log --oneline --graph --all
Visualizes all branches and their relationships.
Comparing Branches
git diff main..feature-branch
Renaming Branches
git branch -m old-name new-name
Branch Best Practices
- Keep
mainstable and deployable - Create branches for each feature/fix
- Merge or delete branches when done
- Pull
mainregularly into long-running feature branches - Use descriptive branch names
- Don't commit directly to
mainon team projects
Next: Timeline Convergence
You can create parallel timelines. Now learn to merge them back together!