Branches and merging
Working with branches
Goal: Create, switch and delete branches · 8 min
A branch is a movable pointer to a commit. It lets you develop a feature in isolation while main stays stable.
git branch # list branches (* marks the current one)
git switch -c login-form # create a branch and switch to it
git switch main # go back to main
git branch -d login-form # delete a merged branch
When you commit, only the current branch pointer moves forward. Creating a branch is instant and cheap — Git just writes a new pointer. Before switching, commit (or stash) your work; Git refuses to switch if uncommitted changes would be overwritten. Older tutorials use git checkout -b, which does the same as git switch -c.
Worked example: A feature branch
- Create and switch to the branch.
- Commit on the branch; main is unchanged.
- Switch back: the files show main's state again.
git switch -c dark-mode
git add theme.css
git commit -m "Add dark colour theme"
git switch main