Branches: Create, Switch, and List
9 min read
What Is a Branch?
A branch is a lightweight, movable pointer to a commit. Creating a branch costs almost nothing — it is just a 41-byte file. Branches let you diverge from main, develop a feature in isolation, and merge back when it is ready.
Working with Branches
bash
git branch # list local branches
git branch -a # list local and remote branches
git branch feature/login # create a new branch
git switch feature/login # switch to it
git switch -c feature/login # create AND switch in one step
git branch -d feature/login # delete a merged branch
git branch -D feature/login # force-delete an unmerged branchThe git switch command replaced the older git checkout for branch operations in recent Git versions. Both work, but switch is clearer in intent.
CPCODELAB Branch Naming Conventions
feature/<short-description>— new featuresfix/<short-description>— bug fixeschore/<short-description>— tooling or maintenancedocs/<short-description>— documentation updates
At CPCODELAB, interns never commit directly to `main`. All work happens on a feature branch. This keeps main stable and ensures every change goes through a pull request review.
Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises