Understanding Branches
What is a Branch?
A branch is a parallel version of your project. It lets you work on new features without touching the main code.
main: A ─── B ─── C
\
feature: D ─── E
In this diagram:
- main has commits A, B, C
- feature branched off from C and has its own commits D, E
- Changes in feature don't affect main until you merge
Why Use Branches?
| Scenario | Without Branches | With Branches |
|---|---|---|
| Try a new feature | Might break working code | Work safely in isolation |
| Fix a bug | Stop all other work | Fix on a separate branch |
| Team collaboration | Overwrite each other's code | Everyone has their own branch |
Common Branch Commands
# See all branches (* marks current)
git branchCreate a new branch
git branch feature-loginSwitch to a branch
git checkout feature-login
OR (newer command)
git switch feature-loginCreate AND switch in one step
git checkout -b feature-login
OR
git switch -c feature-loginDelete a branch
git branch -d feature-login
The main Branch
When you create a repo, Git starts with a default branch called main (or sometimes master). This is your "production" code — the stable version.
Branch Naming Conventions
Good branch names describe what you're working on:
feature/add-login-page
fix/broken-navbar
update/readme-instructions
Summary
| Command | Action |
|---|---|
| <code>git branch</code> | List branches |
| <code>git branch name</code> | Create a branch |
| <code>git switch name</code> | Switch to a branch |
| <code>git switch -c name</code> | Create and switch |
| <code>git branch -d name</code> | Delete a branch |