git switch -c branch-name creates the branch and moves you onto it. The older equivalent is git checkout -b branch-name.Guide
Branches let you build a feature on the side without touching working code. This is where Git shines.
A branch is a separate line of work. The default branch is usually called
main. When you want to add a feature or fix a bug, you create a branch off
main, do your work there, and only merge it back when it's ready. Meanwhile
main stays clean and working.
Mental model: branching is like "Save As" for a whole timeline. You can experiment freely — if it doesn't work out, just delete the branch and main is untouched.
The modern, beginner-friendly command is git switch -c ("create and switch").
You'll also see the older git checkout -b everywhere — they do the same thing.
Start a new branch
$ git switch -c feature-login
$ # older equivalent:
$ git checkout -b feature-login Name branches for what they do: feature-login, fix-navbar, update-readme.
git switch (or older git checkout) moves you between existing
branches. Your files instantly change to match that branch. Tip: commit or stash your work
before switching, so nothing is half-finished.
Move between branches
$ git switch main
$ git switch feature-login See all branches
$ git branch The branch with a star (*) in the terminal output is the one you're on.
Once a branch is merged, delete it to keep the list tidy — its commits live on in
main. Use -d for a merged branch and -D to force-delete one.
Tidy up branches
$ git branch -d feature-login # delete a merged branch
$ git branch -D feature-login # force-delete (even if unmerged)
$ git branch -m old-name new-name # rename a branch You can't delete the branch you're on — switch to main first. -d refuses unmerged branches to protect you; -D overrides that.
git switch - flips you back to the branch you were just on, like a "back" button.
To start working on a branch that exists on GitHub, fetch first, then switch to it by name.
Jump around quickly
$ git switch - # toggle to the previous branch
$ git fetch
$ git switch their-feature # check out a branch that exists on the remote You don't manage these by hand — Git moves them as you commit and switch. Just knowing the words means error messages and tutorials stop looking scary.
Seen "detached HEAD"? That's just HEAD pinned to an old commit instead of a branch — harmless, and one command gets you out. Detached HEAD, Explained covers both ways.
Practice this live: the playground lets you create branches and switch between them with a click, so you can see HEAD and the tips move.
Up next: bringing a branch back into main with merge (and the calmer truth about merge conflicts).
FAQ
git switch -c branch-name creates the branch and moves you onto it. The older equivalent is git checkout -b branch-name.git branch -d branch-name deletes a branch that's been merged; -D force-deletes one that hasn't. You can't delete the branch you're currently on.