Creating and switching branches
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
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:
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 taggit checkout -b <name> is the older equivalent of git switch -c, and still works.
Listing branches
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 feature/comments 4b5a6f7 Add comments section
fix/home-title 8c9d0e1 Fix typo in blog title
* main 1a2b3c4 Create blogYour 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
git switch main
git switch - # the previous branchSwitching 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:
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.
AbortingYour 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
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:
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?
git branch --show-current # just the name
git status # "On branch …" on the first lineHabit: start every task with
git switch main, update it (once you have a remote), thengit switch -c <prefix>/<short-description>.
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.
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 branchesmkdir 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
git switch -c feature/comments
echo "<section>Comments</section>" > comments.html
git add comments.html && git commit -qm "Add comments section"
# 2. Urgent fix on its own branch from main
git switch main # comments.html disappears from the folder
git switch -c fix/title-typo
sed -i.bak 's/Blgo/Blog/' index.html && rm index.html.bak
git commit -qam "Fix typo in blog title"
# 3. Switch around
git switch - # back to the previous branch: main
git switch feature/comments
# 4. Rename the fix branch
git branch -m fix/title-typo fix/home-title
# 5. List branches
git branch -v # every branch and its last commit
git branch --no-merged main # feature/comments, fix/home-titleSummary
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, likecd -.git branch -ddeletes a merged branch;-Dforces deletion of unmerged work.- Name branches by purpose with a prefix:
feature/…,fix/…,docs/….