Branching Basics
What is a Branch?
A branch is a lightweight, movable pointer to a commit. Think of it as a parallel timeline. You can create a branch, make changes there, and the main branch is completely unaffected until you decide to merge.
This is how teams work in parallel — one developer works on a login feature while another fixes a bug, each on their own branch.
Listing Branches
git branch
The current branch is marked with * and shown in green. When you first init a repo you have one branch: main.
Creating a Branch
git branch feature-login
This creates the branch but doesn't switch to it yet. You're still on main.
Switching Branches
The modern command (git 2.23+):
git switch feature-login
The older command (still widely used):
git checkout feature-login
Create and Switch in One Step
git switch -c new-feature # modern git checkout -b new-feature # classic
What is HEAD?
HEAD is just a pointer to the branch you're currently on. When you make a commit, HEAD (and your current branch) moves forward to the new commit.
HEAD → feature-login → commit E
When you switch branches, HEAD moves to point at the new branch.
main on a real project. Always create a feature branch, do your work there, then merge back (or open a pull request).
Deleting a Branch
After merging, clean up old branches:
git branch -d feature-login # safe delete (only if merged) git branch -D feature-login # force delete
Your Challenges
feature: git branch feature
git switch feature
main: git switch main