Creating and switching branches

Topic 2 of 4 in Branching and merging6 min readBeginnerFree course

Overview

This lesson is the practical side of branches: the handful of commands you’ll use every day to create them, move between them, tidy them up — and what happens to your files when you switch.

Creating a branch

Terminal
git branch feature/comments        # create it, but stay where you are
git switch -c feature/comments     # create it AND switch to it (what you usually want)

The new branch starts at the commit you’re on. To start somewhere else, give a starting point:

Terminal
git switch -c fix/login main          # from main's tip, wherever you are now
git switch -c hotfix/2.4.1 v2.4.0     # from a release tag

git checkout -b <name> is the older equivalent of git switch -c, and still works.

Listing branches

Terminal
git branch            # local branches; * marks the current one
git branch -v         # with each branch's latest commit
git branch -a         # include remote-tracking branches (chapter 7)
git branch --merged       # branches whose work is already in the current branch
git branch --no-merged    # branches with commits not yet merged
Text
  feature/comments 4b5a6f7 Add comments section
  fix/home-title   8c9d0e1 Fix typo in blog title
* main             1a2b3c4 Create blog

Your shell prompt can show the current branch too — Git for Windows’ Git Bash does this by default, and tools like Oh My Zsh or Starship add it on macOS and Linux. It saves a lot of “wait, which branch am I on?”.

Switching

Terminal
git switch main
git switch -              # the previous branch

What git switch does: 1, moves HEAD to the other branch; 2, updates files to that branch’s snapshot; 3, keeps your edits if they don’t conflict. Examples: git switch feature goes to an existing branch; git switch -c fix-typo creates it and goes there; git switch - goes back to the previous branch. If switching would overwrite your local changes, Git stops with an error listing the files — commit, stash or restore them first. git checkout <branch> does the same thing; git switch (Git 2.23+) is the clearer modern command.

Switching makes your working directory match the other branch’s latest snapshot. Files that only exist on the other branch appear; files that only exist on this branch disappear (they’re safe in their commits); changed files are updated.

What about uncommitted changes?

  • If your edits don’t conflict with the switch — the files you changed are the same on both branches — Git simply carries them over. They’re not “on” any branch until you commit.
  • If switching would overwrite your edits, Git refuses:
Text
error: Your local changes to the following files would be overwritten by checkout:
	index.html
Please commit your changes or stash them before you switch branches.
Aborting

Your options: commit the work (on a temporary commit if needed), stash it (Stashing work), or discard it with git restore. Git won’t lose your work silently.

Renaming and deleting

Terminal
git branch -m fix/title-typo fix/home-title   # rename a branch
git branch -m new-name                        # rename the current branch

git branch -d fix/home-title                  # delete a branch that has been merged
git branch -D experiment                      # force-delete a branch with unmerged commits

-d refuses if the branch has commits that aren’t merged:

Text
error: the branch 'experiment' is not fully merged.

That’s a safety net. -D overrides it — the commits are then only reachable through the reflog. You can’t delete the branch you’re on; switch away first.

Naming branches

Good names say what the branch is for. Common conventions:

Prefix For Example
feature/ or feat/ new functionality feature/comment-replies
fix/ bug fixes fix/cart-total-rounding
docs/ documentation docs/install-windows
chore/ maintenance chore/upgrade-node-24
issue number links to the tracker fix/482-login-timeout

Rules Git enforces: no spaces, no .., no ~ ^ : ? * [ or backslashes, and names can’t end in / or .lock. Stick to lowercase letters, numbers, - and /.

The / creates a hierarchy, and many tools group branches by prefix. One catch: you can’t have both a branch called feature and one called feature/x.

Where am I?

Terminal
git branch --show-current     # just the name
git status                    # "On branch …" on the first line

Habit: start every task with git switch main, update it (once you have a remote), then git switch -c <prefix>/<short-description>.

Exercise

Juggle three branches

You’re building a blog. (1) Create and switch to feature/comments in one command and commit a comments.html. (2) An urgent fix is needed: switch back to main, create fix/title-typo, fix the typo in index.html and commit. (3) Go back to feature/comments with the shortcut for “the previous branch”… then realise it’s the wrong one and go to feature/comments by name. (4) Rename fix/title-typo to fix/home-title. (5) List all branches with their last commit, and list which ones are not yet merged into main.

Terminal
mkdir blog && cd blog && git init -b main
echo "<h1>My Blgo</h1>" > index.html && git add . && git commit -qm "Create blog"

# 1. New feature branch with a commit

# 2. Urgent fix on its own branch from main

# 3. Switch around

# 4. Rename the fix branch

# 5. List branches

Summary

  • git switch -c <name> creates a branch at the current commit and switches to it.
  • Switching updates your files to match the branch; uncommitted changes come with you if they don’t conflict.
  • git switch - returns to the previous branch, like cd -.
  • git branch -d deletes a merged branch; -D forces deletion of unmerged work.
  • Name branches by purpose with a prefix: feature/…, fix/…, docs/….