Lesson
Branches
Branches are movable pointers. Learn how switching, diverging, and deleting work.
Learning objectives
- Understand a branch as a pointer to a commit
- See how new commits move only the active branch
- Recognize diverging history and detached HEAD
A branch is a pointer
Creating a branch writes a new pointer at the current commit. That is the entire cost — no files are copied. This is why Git branches are cheap and encouraged.
Only the active branch moves
When you commit, only the branch HEAD points at advances. Other branches stay where they are, which is how two branches diverge from a common commit.
Detached HEAD
If you check out a commit hash directly, HEAD points at the commit rather than a branch. Commits you make there belong to no branch and can be lost when you switch away.
Try it yourself
Empty folder
Run git init to create a repository.
Working directory
Clean — no uncommitted changes.
Staging area
Nothing staged. Run git add.
Repository
No commits yet.
Branch off, commit on the feature, then switch back — main is unchanged.
Commands used
git switch -c feature
git add feature.ts
git commit -m "feat"
git switch main
git log --onelineCommon mistakes
- • Committing in detached HEAD and losing the work.
- • Deleting an unmerged branch with -D.
Best practices
- • Use descriptive names: feat/…, fix/…, docs/….
- • Keep branches short-lived and focused.
Mini challenge
Create a branch, add a commit, switch back to main, and confirm main did not move.
Open the visualizer to try itSummary
- Branches are cheap movable pointers.
- Only the checked-out branch advances on commit.
- Detached HEAD commits belong to no branch.