Ravindra BagaleCourses & study guides

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

  1. Open your Chapter 2 hello repo (badge-api-hello or similar).
  2. Make sure you are on main (or master): git switch main.
  3. Confirm a clean working tree: git status.
  4. Create and switch: git switch -c feature/health.
  5. Confirm with git branch — the star should be on feature/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.