What are Branches
Branches are one of Git's most powerful features. They allow you to work on different features, fixes, or experiments in isolation without affecting the main codebase.
Understanding Branches
A branch is simply a pointer to a commit. When you create a branch, Git creates a new pointer—it doesn't copy any files.
main: A ← B ← C ← D
↑
main (pointer)
The default branch (usually main or master) is just a branch like any other.
Why Use Branches?
| Benefit | Description |
|---|---|
| Isolation | Work on features without affecting stable code |
| Parallel development | Multiple features developed simultaneously |
| Experimentation | Try ideas without risk |
| Code review | Review changes before merging |
| Releases | Maintain different versions |
How Branches Work
Before Creating a Branch
A ← B ← C ← D
↑
main
HEAD
HEAD points to the current branch (main), which points to commit D.
After Creating a Feature Branch
A ← B ← C ← D
↑
main
feature ← HEAD
Both main and feature point to the same commit (D).
After Making Commits on Feature
A ← B ← C ← D
↑ ↖
main E ← F
↑
feature
HEAD
feature has moved forward with new commits E and F. main stays where it was.
The HEAD Pointer
HEAD is a special pointer that indicates your current position in the repository:
- Usually points to a branch name (e.g., HEAD → main → commit D)
- When you commit, the current branch moves forward
- When you checkout a branch, HEAD moves to that branch
Detached HEAD
If HEAD points directly to a commit (not a branch), you're in "detached HEAD" state:
git checkout abc1234 # Detached HEAD
This is useful for looking at old commits, but be careful—commits made here might be lost if you don't create a branch.
Visualizing Branches
# See all branches
git branch
# See branches with last commit
git branch -v
# See all branches including remote
git branch -a
# Visual log with branches
git log --oneline --graph --all
Output of git log --oneline --graph --all:
* 7d3f8e2 (HEAD -> feature) Add login form
* 5b2c1a0 Start login feature
| * 9c4d3b2 (main) Fix homepage bug
|/
* 1a2b3c4 Initial commit
Branch Naming
Branches can have almost any name, but follow these conventions:
Common Prefixes
| Prefix | Use |
|---|---|
feature/ | New features |
bugfix/ | Bug fixes |
hotfix/ | Urgent production fixes |
release/ | Release preparation |
docs/ | Documentation |
test/ | Testing |
Examples
feature/user-authentication
bugfix/login-redirect
hotfix/security-patch
release/v2.0
docs/api-documentation
Naming Best Practices
✅ Good:
feature/add-user-loginbugfix/fix-null-pointerissue-123-add-search
❌ Bad:
my-branch(not descriptive)test(too vague)johns-stuff(not professional)fix bug(spaces not allowed)
Exercise: Understand Branches
Explore branches in the repository:
Branches are Cheap
Unlike some version control systems, Git branches are:
- Fast to create: Just creates a 41-byte file (pointer)
- Fast to switch: Just updates a few pointers
- Space-efficient: No file duplication
This is why Git encourages using branches liberally.
Common Branch Workflows
Simple Feature Branch
main: A ── B ── C ────────── M
\ /
feature: D ── E ── F ──
- Create branch from main
- Develop feature
- Merge back to main
Long-Running Branches
main: ○ ── ○ ── ○ ── ○ ── ○ ── ○
\ / \
develop: ○ ── ○ ── ○ ○ ── ○
\ /
feature: ○ ── ○
mainis always stable/productiondevelopfor integrationfeaturebranches for specific work
Summary
- A branch is a lightweight pointer to a commit
- Branches allow isolated, parallel development
- HEAD points to your current position
- Use descriptive names with prefixes
- Git branches are fast and cheap to create
- Branches are fundamental to collaboration workflows
In the next lesson, we'll learn how to create and switch between branches.

