Understanding Branches
Learn about Understanding Branches
Branching is Git's superpower. A branch is an independent line of development — a parallel universe where you can experiment, build features, and fix bugs without affecting the main codebase. Branches in Git are incredibly lightweight (they're just 40-byte pointers to a commit), so creating and switching between them is nearly instantaneous.
Why Branches?
- Isolation — Work on a feature without breaking the main code
- Parallel work — Multiple developers work on different features simultaneously
- Experimentation — Try risky ideas in a branch; delete it if they don't work
- Code review — Branches enable Pull Requests for team review
The main Branch
By default, Git creates a branch called main (or master in older repos). This is your "production-ready" branch — it should always contain working, stable code. All new features and fixes are developed in separate branches and merged into main when ready.
Creating Branches
# Create a new branch (doesn't switch to it)
git branch feature/login
# Create AND switch to a new branch
git checkout -b feature/login
# Newer syntax (same thing)
git switch -c feature/login
# Create a branch from a specific commit
git branch bugfix/nav a1b2c3d
Listing Branches
# List local branches (* marks current)
git branch
# Output:
# feature/login
# * main
# bugfix/navigation
# List remote branches
git branch -r
# List ALL branches (local + remote)
git branch -a
# List with last commit info
git branch -v
How Branches Work
A branch is just a pointer to a commit. When you create a branch, Git creates a new pointer at the current commit. As you make commits on that branch, the pointer moves forward. The HEAD pointer tells Git which branch you're currently on.
# Visual representation:
#
# main: A - B - C
# \
# feature/login: D - E (HEAD is here)
#
# Commits A-C are shared. D and E only exist on feature/login.
Branch Naming Conventions
# Feature branches
feature/user-authentication
feature/shopping-cart
feature/dark-mode
# Bug fix branches
bugfix/login-error
bugfix/cart-total
# Hotfix branches (urgent production fixes)
hotfix/security-patch
hotfix/crash-fix
# Release branches
release/v2.0.0
release/v1.5.1
# Chore/maintenance
chore/update-dependencies
chore/refactor-api
Renaming Branches
# Rename the current branch
git branch -m new-name
# Rename a different branch
git branch -m old-name new-name
What's Next
You understand what branches are and how to create them. In the next episode, we'll learn how to switch between branches and manage your working directory while switching.