Episode 8 of 12
Branches
Learn how to create and switch between branches for parallel development.
Branches let you work on different features or fixes in parallel without affecting the main codebase.
What is a Branch?
A branch is an independent line of development. The default branch is usually called main (or master). You create new branches to work on features, then merge them back.
Viewing Branches
# List all local branches
git branch
# List all branches (including remote)
git branch -a
Creating a Branch
# Create a new branch
git branch feature-navbar
# Create and switch to it immediately
git checkout -b feature-navbar
# Modern syntax
git switch -c feature-navbar
Switching Branches
git checkout main
git checkout feature-navbar
# Modern syntax
git switch main
git switch feature-navbar
Deleting a Branch
# Delete a merged branch
git branch -d feature-navbar
# Force delete an unmerged branch
git branch -D feature-navbar
Typical Workflow
# 1. Start on main
git switch main
# 2. Create a feature branch
git switch -c add-contact-page
# 3. Work and commit on the feature branch
git add .
git commit -m "Add contact page layout"
git commit -m "Add contact form validation"
# 4. Switch back to main
git switch main
# 5. Merge the feature
git merge add-contact-page
# 6. Delete the feature branch
git branch -d add-contact-page
Branch Naming Conventions
feature/user-auth
bugfix/login-error
hotfix/security-patch
release/v2.0