Git Notes
Learn how to switch between branches using git checkout and git switch. Understand branch switching behavior, stashing changes, and handling uncommitted work.
Switching branches changes your working directory to reflect the state of the target branch. Git provides two commands for this: the traditional git checkout and the modern git switch.
Switching with git switch (Recommended)
The git switch command was introduced in Git 2.23 specifically for branch switching:
git switch branch-nameSwitched to branch 'branch-name'
Create and Switch
git switch -c new-branch-nameSwitched to a new branch 'new-branch-name'
Switch to Previous Branch
git switch -Switched to branch 'main'
Switching with git checkout (Traditional)
git checkout branch-nameSwitched to branch 'branch-name'
Create and Switch
git checkout -b new-branch-namegit switch vs git checkout
| Feature | git switch | git checkout |
|---|---|---|
| Switch branches | ✅ git switch main | ✅ git checkout main |
| Create + switch | ✅ git switch -c new | ✅ git checkout -b new |
| Restore files | ❌ Use git restore | ✅ git checkout -- file |
| Detached HEAD | git switch --detach | git checkout commit |
| Clarity | Single purpose | Overloaded (branches + files) |
git switch is preferred because it has a single clear purpose — switching branches.
What Happens When You Switch
When you switch branches, Git:
- Updates files in the working directory to match the target branch
- Moves the HEAD pointer to the target branch
- Updates the staging area to match the target branch
| HEAD | feature → [commit C] |
| After | git switch main |
| HEAD | main → [commit B] |
Handling Uncommitted Changes
Clean Working Directory (No Issues)
git switch developSwitched to branch 'develop'
Uncommitted Changes That Don't Conflict
If your uncommitted changes don't conflict with the target branch, Git carries them along:
# You have changes in file-A.txt (not modified on develop)
git switch developM file-A.txt Switched to branch 'develop'
Uncommitted Changes That Conflict
git switch mainerror: Your local changes to the following files would be overwritten by checkout:
app.js
Please commit your changes or stash them before you switch branches.
AbortingSolutions for Blocked Switch
Option 1: Commit Your Work
git add .
git commit -m "WIP: save progress on feature"
git switch mainOption 2: Stash Changes
git stash
git switch main
# ... do work on main ...
git switch feature-branch
git stash popOption 3: Discard Changes
# Discard all uncommitted changes (CAREFUL - permanent!)
git checkout -- .
git switch mainOption 4: Force Switch (Discards Changes)
git switch -f main
# or
git checkout -f mainSwitching to Remote Branches
First Time (Create Local Tracking Branch)
git switch remote-branch-name
# Git auto-creates local branch tracking origin/remote-branch-nameBranch 'remote-branch-name' set up to track remote branch 'remote-branch-name' from 'origin'. Switched to a new branch 'remote-branch-name'
If Multiple Remotes Have Same Branch Name
git switch -c local-name origin/branch-nameDetached HEAD State
Switching to a commit (not a branch) puts you in detached HEAD:
git switch --detach abc1234
# or
git checkout abc1234HEAD is now at abc1234 Some commit message
In detached HEAD, new commits are not on any branch. To keep work done here:
# Create a branch before switching away
git switch -c save-my-workPractical Workflows
Quick Context Switch
# Working on feature, need to check something on main
git stash
git switch main
# ... check something ...
git switch - # Back to feature
git stash popReview a Colleague's Branch
git fetch origin
git switch colleague/feature-branch
# Review their code
git switch - # Back to your branchSwitch Between Multiple Features
git switch feature/auth # Work on auth
git switch feature/dashboard # Switch to dashboard
git switch feature/auth # Back to auth
git switch - # Quick toggle to last branchInterview Questions
- What is the difference between
git switchandgit checkout?
git switch is dedicated to branch switching only (introduced in Git 2.23). git checkout is overloaded — it switches branches AND restores files. git switch is clearer and less error-prone.
- What happens to uncommitted changes when you switch branches?
If changes dont conflict with the target branch, they are carried along. If they conflict, Git blocks the switch and asks you to commit or stash changes first.
- What is a detached HEAD state?
Detached HEAD means HEAD points directly to a commit instead of a branch. Any new commits made in this state will be orphaned when you switch to another branch, unless you create a branch first.
- How do you quickly switch to the previous branch?
Use git switch - or git checkout -. The dash refers to the previously checked-out branch, similar to cd - in shell.
- How do you switch to a remote branch you have never checked out locally?
Simply use git switch branch-name. If a matching remote branch exists, Git automatically creates a local tracking branch and switches to it.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Switch Branch in Git - Navigate Between Branches.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, switch
Related Git & GitHub Topics