Creating and Switching Branches
Now that you understand what branches are, let's learn how to create, switch between, and manage them.
Creating a New Branch
git branch
Create a new branch:
git branch feature-login
This creates the branch but doesn't switch to it. You're still on your current branch.
A ← B ← C ← D
↑
main ← HEAD
feature-login
git checkout -b (Classic Way)
Create and switch in one command:
git checkout -b feature-login
git switch -c (Modern Way)
The newer, more intuitive command:
git switch -c feature-login
Both do the same thing—create the branch and switch to it.
Switching Branches
git checkout (Classic)
git checkout feature-login
git switch (Modern)
git switch feature-login
Why git switch?
git checkout does many things (switch branches, restore files, etc.). git switch is dedicated to branch switching, making it clearer and safer.
# These do the same thing:
git checkout main
git switch main
# But only checkout can restore files:
git checkout -- file.js # Restore file
# git switch -- file.js # Doesn't work
Creating Branches from Specific Points
From Another Branch
# Create branch from main (while on any branch)
git branch feature-x main
# Create and switch
git switch -c feature-x main
From a Specific Commit
# Create branch from commit
git branch bugfix abc123
# Create and switch
git switch -c bugfix abc123
From a Tag
git switch -c hotfix v1.0.0
Listing Branches
# List local branches
git branch
# List with more info
git branch -v
# List all branches (including remote)
git branch -a
# List remote branches only
git branch -r
# List branches containing a commit
git branch --contains abc123
# List merged branches
git branch --merged
# List unmerged branches
git branch --no-merged
Output of git branch -v:
feature-login abc1234 Add login form
* main def5678 Update homepage
bugfix-nav 789abcd Fix navigation
The * indicates the current branch.
Renaming Branches
# Rename current branch
git branch -m new-name
# Rename specific branch
git branch -m old-name new-name
# Force rename (even if new name exists)
git branch -M new-name
Renaming the Default Branch
# Rename master to main
git branch -m master main
# Update remote (if applicable)
git push -u origin main
git push origin --delete master
Deleting Branches
Delete Merged Branch
git branch -d feature-login
Git will refuse if the branch isn't merged:
error: The branch 'feature-login' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-login'.
Force Delete
git branch -D feature-login
Delete Remote Branch
git push origin --delete feature-login
# or
git push origin :feature-login
Switching with Uncommitted Changes
If you have uncommitted changes and try to switch:
Case 1: No Conflict
If your changes don't conflict with the target branch, Git carries them over:
# Make changes to file.js
git switch other-branch
# Changes come with you
Case 2: Conflict
If changes would be overwritten, Git prevents the switch:
error: Your local changes to the following files would be overwritten by checkout:
file.js
Please commit your changes or stash them before you switch branches.
Solutions:
# Option 1: Commit your changes
git commit -am "WIP: save work"
git switch other-branch
# Option 2: Stash your changes
git stash
git switch other-branch
git stash pop # Later, restore changes
# Option 3: Discard changes (careful!)
git restore .
git switch other-branch
Exercise: Create and Switch Branches
Practice creating and switching branches:
The Detached HEAD State
Sometimes you need to look at an old commit:
git checkout abc123
You'll see:
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
Working in Detached HEAD
You can make commits, but they're not on any branch:
git checkout abc123
# Make changes
git commit -m "Experimental change"
# This commit is "orphaned"
Keeping Detached HEAD Commits
If you want to keep commits made in detached HEAD:
# While still in detached HEAD
git switch -c my-experiment
# Now your commits are on a branch
Or, if you already switched away:
# Find the commit in reflog
git reflog
# Create branch pointing to it
git branch rescue abc123
Summary
- Use
git branch nameto create a branch - Use
git switch -c nameto create and switch (orgit checkout -b name) - Use
git switch nameto switch branches - Use
git branch -dto delete merged branches,-Dto force delete - Use
git branch -mto rename branches - Handle uncommitted changes with commit, stash, or discard
- Avoid making commits in detached HEAD state unless intentional
In the next lesson, we'll learn how to merge branches together.

