What are Branches?
A branch is an independent line of development. It lets you work on a feature, bug fix, or experiment without affecting the main codebase.
The Big Picture
Think of branches like parallel timelines:
main: A --- B --- C
\
feature: D --- E
maincontinues forward with commitCfeaturebranched off fromBand has its own commitsDandE- They don't interfere with each other
Why Use Branches?
Isolation — Work on a feature without breaking main. If the experiment fails, just delete the branch.
Collaboration — Multiple developers work on different branches simultaneously, then merge when ready.
Code Review — Create a branch, push it, and open a pull request for others to review before merging.
How Branches Work Internally
A branch is just a pointer to a commit. That's it. Creating a branch doesn't copy any files — it just creates a new pointer.
main ──→ commit C
feature ──→ commit E
HEAD ──→ feature (you're currently on "feature")
HEADis a special pointer that tracks which branch you're currently on- When you commit, the current branch pointer moves forward
- When you switch branches,
HEADmoves to the other branch
The Default Branch
When you run git init, Git creates a default branch called main (or master in older versions). This is just a convention — there's nothing technically special about it.
Common Branch Workflow
- Create a branch:
git branch feature - Switch to it:
git checkout feature - Make commits on the branch
- Switch back:
git checkout main - Merge the work:
git merge feature - Delete the branch:
git branch -d feature
In the next lessons, you'll practice each of these steps.