Branches let you work on a feature, a bug fix or an experiment in isolation, without disturbing the stable code on main or anyone else's work. In Git, branches are so cheap that teams create one for every change, often several a day. This lesson explains what a branch really is, how to create, switch, list, rename and delete them, and what HEAD means.
In simple words
- Branch: a separate line of work. You can change things on a branch without touching the main code.
- main: the main branch, usually holding the working, released code.
- git switch: moves you to another branch, or creates one with -c.
- Detached HEAD: looking at an old commit directly, not on any branch. New commits here are easy to lose.
What a branch is
A branch is nothing more than a named pointer to a commit. It is stored as a tiny file containing a 40-character hash. When you commit on a branch, the pointer moves forward to the new commit automatically.
c1 ← c2 ← c3 main → c3HEAD is a pointer to the branch you are currently on. That is how Git knows which branch should move when you commit.
c1 ← c2 ← c3 main → c3 ← HEADCreating a branch just adds another pointer; no files are copied. That is why it is instant even in huge repositories.
Set up
git config --global user.name "Leela Kumar"mkdir branch-labcd branch-labgit initprintf 'def health():\n return "ok"\n' > app.pygit add app.pygit commit -m "Add health check"Create and switch: git switch
git branchgit switch -c feature/cartgit branch* mainSwitched to a new branch 'feature/cart'* feature/cart maingit switch -c NAME creates a branch at the current commit and switches to it. The * marks the current branch. (The older form git checkout -b NAME does the same; switch was introduced to separate branch operations from file operations.)
Now commit on the new branch:
printf 'def cart():\n return []\n' > cart.pygit add cart.pygit commit -m "Add empty cart"git log --oneline --all --graph[feature/cart 8b2d4e1] Add empty cart 1 file changed, 2 insertions(+) create mode 100644 cart.py* 8b2d4e1 (HEAD -> feature/cart) Add empty cart* 3f9a0c2 (main) Add health checkfeature/cart moved forward; main stayed where it was.
Switching changes your files
When you switch branches, Git updates the working tree to match that branch's latest commit:
lsgit switch mainlsgit switch feature/cartlsapp.py cart.pySwitched to branch 'main'app.pySwitched to branch 'feature/cart'app.py cart.pycart.py disappeared on main and came back on feature/cart. Nothing was lost; each branch simply shows its own snapshot.
Switching with uncommitted changes
Uncommitted changes travel with you when you switch, as long as they do not conflict with the target branch. If they would be overwritten, Git refuses:
error: Your local changes to the following files would be overwritten by checkout: app.pyPlease commit your changes or stash them before you switch branches.AbortingCommit the work, or put it aside with git stash (lesson 10), then switch.
Diverging branches
Go back to main and commit something different there too:
git switch mainprintf 'def health():\n return {"status": "ok"}\n' > app.pygit commit -am "Return JSON from health check"git log --oneline --all --graphSwitched to branch 'main'[main 6c1e7a5] Return JSON from health check 1 file changed, 1 insertion(+), 1 deletion(-)* 6c1e7a5 (HEAD -> main) Return JSON from health check| * 8b2d4e1 (feature/cart) Add empty cart|/* 3f9a0c2 Add health checkThe two branches have diverged: each has a commit the other does not. Combining them is called merging, the subject of the next lesson.
Listing and inspecting branches
git branch -vgit branch --mergedgit branch --no-merged feature/cart 8b2d4e1 Add empty cart* main 6c1e7a5 Return JSON from health check* main feature/cart-vshows the latest commit on each branch.--mergedlists branches whose work is already in the current branch (safe to delete).--no-mergedlists branches with work not yet merged.-aalso shows remote-tracking branches likeorigin/main(next lessons).
Renaming and deleting
git branch -m feature/cart feature/shopping-cartgit branchgit branch -d feature/shopping-cartgit branch -D feature/shopping-cart feature/shopping-cart* mainerror: the branch 'feature/shopping-cart' is not fully mergedhint: If you are sure you want to delete it, run 'git branch -D feature/shopping-cart'Deleted branch feature/shopping-cart (was 8b2d4e1).-d refuses to delete a branch with unmerged work, a useful safety net. -D deletes it anyway. Even then, the commits can usually be recovered from the reflog for a while (lesson 15).
Detached HEAD
If you switch to a commit instead of a branch, HEAD points directly at that commit. This is called detached HEAD:
git switch --detach HEAD~1git statusgit switch mainHEAD is now at 3f9a0c2 Add health checkHEAD detached at 3f9a0c2nothing to commit, working tree cleanSwitched to branch 'main'It is useful for looking at old versions. If you make commits while detached and then switch away, those commits are not on any branch. Create a branch first (git switch -c experiment) if you want to keep them.
Naming branches
Teams usually agree on a convention so branches are easy to scan and CI can react to them:
| Prefix | For |
|---|---|
feature/ or feat/ | new functionality: feature/cart-discounts |
fix/ or bugfix/ | bug fixes: fix/gst-rounding |
hotfix/ | urgent production fixes |
chore/, docs/, refactor/ | maintenance |
| ticket numbers | feature/SHOP-142-discounts |
Use lowercase and hyphens; avoid spaces and special characters.
Real-world scenario: two urgent requests
Leela is halfway through a discount feature on feature/discounts when a production bug report arrives. She commits her work-in-progress, runs git switch main, git pull, then git switch -c fix/checkout-timeout, fixes the bug and opens a merge request. Twenty minutes later she runs git switch feature/discounts and continues exactly where she was. The bug fix shipped without a single line of unfinished feature code, because each change lived on its own branch.
Common mistakes
- Committing directly on
mainwhen the team works with merge requests. Create a branch first. - Long-lived branches that drift far from
mainand become painful to merge. Keep branches small and short. - Forgetting which branch you are on. Check
git statusor show the branch in your shell prompt (the practice terminal does). - Deleting with
-Dwithout checkinggit branch --no-merged. - Committing in detached HEAD and losing track of the commits.
Practice
- Create a branch
feature/login, add a file on it and commit. - Switch back to
mainand confirm the file is not there. - Make a commit on
mainand view both branches withgit log --oneline --all --graph. - Rename
feature/logintofeature/sign-in. - Try to delete it with
-dand read the message.
Cheat sheet
git branch list local branchesgit branch -v | -a | --merged | --no-mergedgit switch -c name [start] create and switchgit switch name switchgit switch - back to the previous branchgit switch --detach <commit> look at an old commitgit branch -m old new renamegit branch -d name delete (merged only)git branch -D name force deletegit log --oneline --all --graph see all branchesKey takeaways
- A branch is a lightweight pointer to a commit;
HEADpoints to the branch you are on. git switch -ccreates a branch; committing moves only the current branch forward.- Switching branches updates your files to that branch's snapshot.
- Branches diverge when both get new commits; merging combines them.
- Use clear naming conventions and keep branches short-lived.