
A branch is just a pointer to a commit. That's it.
No copy of your code, no separate folder, no magic. Just a tiny file that says "I'm pointing at commit abc123."
When you create a branch, Git creates that pointer. When you switch branches, Git moves HEAD to point at the new branch. When you commit, the branch pointer moves forward.
That's the entire model.
Why branches exist
Without branches, you'd have one linear history. Every change would stack on top of the previous one. Want to try something experimental? Too bad, it's now part of your main code.
Branches let you diverge. You can work on a feature, break things, experiment, and your main code stays untouched.
Two parallel timelines. You pick which one to follow.
Creating branches
# Create a branch
git branch feature-login
# Create and switch to it
git checkout -b feature-login
# Or with newer Git
git switch -c feature-login
TIP
Use git switch -c over git checkout -b. It's clearer and designed specifically for branches.
The branch is created from wherever HEAD is pointing. Usually your current branch's latest commit.
Switching branches
# Old way
git checkout main
# New way
git switch main
When you switch, Git updates your working directory to match that branch's latest commit. Your files literally change on disk.
WARNING
If you have uncommitted changes, Git might refuse to switch or might carry those changes over. Either commit, stash, or discard them first.
Listing branches
# Local branches
git branch
# All branches (including remote)
git branch -a
# With last commit info
git branch -v
The current branch has a * next to it.
Deleting branches
# Delete a merged branch
git branch -d feature-login
# Force delete (even if not merged)
git branch -D feature-login
NOTE
You can't delete the branch you're currently on. Switch to another branch first.
🔍 What happens when you delete a branch?
Git just removes the pointer. The commits are still there, at least temporarily. Git's garbage collector will eventually clean up commits that nothing points to, but you have some time to recover if you made a mistake.
To recover a deleted branch:
git reflog
# Find the commit hash
git branch recovered-branch abc123
Remote branches
Your local branches and remote branches are separate things. When you clone a repo, you get local copies of remote branches.
# See remote branches
git branch -r
# Fetch updates from remote
git fetch origin
# Push a local branch to remote
git push origin feature-login
# Push and set upstream (so you can just git push next time)
git push -u origin feature-login
Remote branches are named origin/main, origin/feature-login, etc. You can't commit to them directly, you commit locally and push.
Tracking branches
A tracking branch is a local branch connected to a remote branch.
# Create a local branch that tracks a remote branch
git checkout -b feature-login origin/feature-login
# Or shorter
git checkout feature-login
# Git auto-tracks if a remote branch with the same name exists
# See tracking info
git branch -vv
When you have tracking set up, git pull and git push know where to go without you specifying.
Branch naming conventions
No strict rules, but common patterns:
feature/login-page— new featurebugfix/header-alignment— bug fixhotfix/security-patch— urgent production fixrelease/v2.1.0— release preparation
Some teams use prefixes, some don't. Pick a convention and stick with it.
TIP
Keep branch names lowercase with hyphens. Avoid spaces, special characters, and overly long names.
Common mistakes
"I committed to the wrong branch"
# Undo the commit but keep changes
git reset --soft HEAD~1
# Switch to the right branch
git switch correct-branch
# Commit there
git commit -m "Your message"
"I need to switch but have uncommitted changes"
# Stash your changes
git stash
# Switch branches
git switch other-branch
# Do your thing, then come back
git switch original-branch
# Get your changes back
git stash pop
"My branch is behind main"
# Option 1: Merge main into your branch
git switch feature-branch
git merge main
# Option 2: Rebase (cleaner history)
git switch feature-branch
git rebase main
We'll cover merge vs rebase in depth in the next article.
Cheat sheet
| Command | What it does |
|---|---|
git branch | List local branches |
git branch name | Create a branch |
git switch name | Switch to a branch |
git switch -c name | Create and switch |
git branch -d name | Delete merged branch |
git branch -D name | Force delete branch |
git push -u origin name | Push and track |
git fetch | Update remote branches |
The takeaway
"Branch early, branch often."
Branches are cheap. Creating one takes milliseconds and barely any disk space. Don't overthink it, just create a branch whenever you're doing something that might not work out.
Next article: merge, rebase, and squash. What happens when branches need to come back together.
This is part 2 of my "Git & GitHub in 10 articles" series.