Creating and Switching Branches

Updated

October 30, 2026

Branch to Create a Safe Working Area

After this page you can create, switch, list, and delete branches on Linux without touching main.

A branch is a name

cd ~/desk/desk-api
git branch
git switch -c feat/ping-json
git branch

git switch -c creates feat/ping-json at current HEAD and points HEAD at it. git branch (no args) lists local names; * is where HEAD is.

git switch main
git switch feat/ping-json

git switch is the modern command. git checkout still works; this book uses switch for branches.

Name them so later pages stay readable

feat/…     a capability
fix/…      a bug
chore/…    tooling

Slashes are fine on Linux. The name is a file under .git/refs/heads/ (or a line in packed-refs). feat/ping-json is the path refs/heads/feat/ping-json.

Work on the branch

git switch feat/ping-json
# edit, add, commit as in chapter 3
git log --oneline --graph --all --decorate

main stays where you left it. Commits on feat/ping-json are extra nodes in the DAG.

 A ← B ← C                 main
           ↖
             D ← E         feat/ping-json   HEAD

List and see

git branch -v                 # last commit on each
git branch --show-current
git log --oneline main..HEAD  # commits on this branch that main does not have

A..B means reachable from B, not from A.

Delete

git switch main
git branch -d feat/ping-json     # refuses if not merged
git branch -D feat/ping-json     # delete anyway

-d is the default you want. -D is when you meant to throw the line of work away.

Rename

git branch -m feat/ping-json feat/ping

Renames the current branch if you omit the old name. After you publish a branch, renaming locally does not rename it on the remote; chapter 11.

Stuck with a dirty working tree

Git refuses to switch if the switch would overwrite uncommitted edits. Commit, stash (chapter 12), or restore first.

git status
git switch main