Git branching cheat sheet

A concise branch workflow for creating, switching, publishing, comparing, merging, renaming, and safely deleting Git branches.

Updated Command scenarios locally verified

Inspect branches

git branch
git branch -r
git branch -vv
git log --oneline --graph --decorate --all -20

Create and switch

git switch -c feature/example
git switch main
git switch --track origin/example

Commit or stash conflicting working-tree changes before switching.

Publish and track

git push -u origin HEAD
git status --short --branch

Confirm git remote -v before the first push.

Compare and integrate

git diff main...feature/example
git log --oneline main..feature/example
git switch main
git merge --no-ff feature/example

The repository may prefer a fast-forward, merge commit, squash, or rebase workflow. Follow its policy.

Rename and delete

git branch -m new-name
git branch --merged main
git branch -d feature/example

Lowercase -d refuses to delete an unmerged branch. Uppercase -D overrides that safety check and can make unique commits harder to find, so inspect first. Generate consistent names with the branch name generator.

Sources and review

Last reviewed September 4, 2026. Technical claims are based on primary documentation and local command verification.