Branches

Why branch?

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.

main feature
A 'feature' branch splits off from main, so work happens in parallel without affecting it.

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.

Create and switch to a branch

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

Terminal
$ git switch -c feature-login
$ # older equivalent:
$ git checkout -b feature-login
In VS Code
  1. Click the branch name in the bottom-left status bar
  2. Choose Create new branch…
  3. Type a name and press Enter — you're now on it

Name branches for what they do: feature-login, fix-navbar, update-readme.

Switching between branches

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

Terminal
$ git switch main
$ git switch feature-login
In VS Code
  1. Click the branch name in the status bar
  2. Pick the branch you want from the list

Listing branches

See all branches

Terminal
$ git branch
In VS Code
  1. Click the branch name in the status bar — the list shows every branch
  2. The current one is marked

The branch with a star (*) in the terminal output is the one you're on.

Deleting and renaming branches

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

Terminal
$ 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
In VS Code
  1. Status-bar branch picker, or the Git Graph extension → right-click a branch → Delete / Rename

You can't delete the branch you're on — switch to main first. -d refuses unmerged branches to protect you; -D overrides that.

Quick switches

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

Terminal
$ git switch -                 # toggle to the previous branch
$ git fetch
$ git switch their-feature     # check out a branch that exists on the remote
In VS Code
  1. The status-bar picker lists remote branches too — choosing one checks it out locally

HEAD and the "tip" — the jargon decoded

  • Tip — the newest commit on a branch. When you commit, the tip moves forward to your new commit.
  • HEAD — a pointer to "where you are right now," normally the tip of your current branch. When you switch branches, HEAD moves with you.

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

Frequently Asked Questions

How do I create a new branch?
git switch -c branch-name creates the branch and moves you onto it. The older equivalent is git checkout -b branch-name.
How do I delete a branch?
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.
What are HEAD and the branch tip?
The tip is the newest commit on a branch. HEAD is a pointer to where you are right now — normally the tip of your current branch.
Will switching branches lose my work?
Commit or stash uncommitted changes first. Git warns you if a switch would overwrite unsaved edits, so you won't lose them by accident.