Chapter 3: Branching, Merge and GitHub PRs
3.2 branch, switch, list — commands
Why. You need a safe place to experiment without touching main.
git branch # list local branches (* = current)
git branch feature/health # create a branch (does not switch yet)
git switch feature/health # move HEAD to that branch
# older style still seen in docs:
# git checkout -b feature/health
What you see: after git switch, git status or git branch shows you are on feature/health. New commits go on this line until you switch again.
| Command | Effect |
|---|---|
git branch |
List local branches |
git branch <name> |
Create branch at current commit |
git switch <name> |
Move to an existing branch |
git switch -c <name> |
Create and switch in one step |
git branch -d <name> |
Delete a merged branch (safe delete) |
Naming habit. Use short, clear names: feature/health, fix/badge-typo, chore/gitignore. Avoid spaces.
Steps — create your first feature branch
- Open your Chapter 2 hello repo (
badge-api-helloor similar). - Make sure you are on
main(ormaster):git switch main. - Confirm a clean working tree:
git status. - Create and switch:
git switch -c feature/health. - Confirm with
git branch— the star should be onfeature/health.
Practice task
Write three good branch names and three bad ones for a badge API change. Explain why the bad ones confuse a reviewer.