git switch / git checkout: Switching Between Branches (Modern vs Legacy)
Creating a branch (previous lesson) is only half the story — you also need to actually move your working directory and HEAD onto it. This lesson covers `git switch`, the modern, clearly scoped command introduced specifically for this purpose, alongside `git checkout`, the older, more overloaded command that still remains extremely common in real-world usage and documentation.
Learning Objectives
- Switch to an existing branch using git switch.
- Create and switch to a new branch in one step using git switch -c.
- Understand the legacy git checkout <branch> and git checkout -b <branch> equivalents.
- Explain why git switch was introduced despite git checkout already existing.
Key Terms to Know Before Learning git switch / git checkout
- git switch: The modern command specifically for changing your currently checked-out branch, introduced in Git 2.23.
- git switch -c <name>: Creates a new branch and switches to it in a single step ('c' for create).
- git checkout <branch>: The legacy syntax for switching branches, still widely used and fully functional.
- git checkout -b <name>: The legacy equivalent of git switch -c, creating and switching to a new branch in one step.
- Working directory update: The process by which Git updates the actual files on disk to match the target branch's latest commit when switching.
How git switch / git checkout Actually Works
To switch to an already-existing branch using the modern syntax:
```
git switch feature/login
```
This moves HEAD to point at `feature/login`, and Git updates your working directory's files to match exactly what that branch's latest commit contains. If you have uncommitted changes that would conflict with files being updated, Git will refuse the switch and ask you to commit, stash (covered later in this module), or discard those changes first — protecting you from silently losing work.
A very common combined operation is creating a brand-new branch and immediately switching to it, which `git switch` supports with the `-c` (create) flag:
```
git switch -c feature/dark-mode
```
This is equivalent to running `git branch feature/dark-mode` followed by `git switch feature/dark-mode`, but as one convenient step.
Before Git 2.23 introduced `git switch` (alongside `git restore`, covered in Module 2), all of this was handled by the single, heavily overloaded `git checkout` command — which was also simultaneously used for discarding file changes (Module 2, Lesson 5). The legacy syntax for the same operations:
```
git checkout feature/login # switch to an existing branch
git checkout -b feature/dark-mode # create AND switch to a new branch in one step
```
Both `git checkout <branch>` and `git switch <branch>` accomplish exactly the same underlying operation for this specific purpose, and you will encounter `git checkout` constantly in older tutorials, Stack Overflow answers, existing scripts, and among developers who learned Git before 2019. Git has no plans to remove `checkout` — it remains fully supported — but `git switch` is now the officially recommended command for this purpose specifically because it's unambiguous: unlike `checkout`, it can only be used for switching (and creating) branches, not for discarding file changes, eliminating an entire class of historical confusion and occasional accidental data loss where someone meant to switch branches but instead discarded changes to a similarly-named file.
A practical safety note either way: if you have uncommitted changes in your working directory that would be overwritten by the branch you're switching to, Git will refuse the operation and print a clear error rather than silently discarding your work — but it's still good practice to run `git status` before switching to understand exactly what state you're in.
git switch / git checkout: Visual Walkthrough
Draw two parallel columns. LEFT labeled 'Modern (Git 2.23+)': 'git switch <branch>' (switch to existing) and 'git switch -c <new-branch>' (create + switch). RIGHT labeled 'Legacy (pre-2.23, still fully supported)': 'git checkout <branch>' (switch to existing) and 'git checkout -b <new-branch>' (create + switch). Draw an equals sign between each corresponding pair, with a note beneath: 'git checkout is also overloaded for discarding file changes (see Module 2) — git switch is NOT, which is exactly why it was introduced.'
git switch / git checkout: Quick Reference Table
| Action | Modern Command | Legacy Command |
|---|---|---|
| Switch to an existing branch | git switch <branch> | git checkout <branch> |
| Create a new branch AND switch to it | git switch -c <branch> | git checkout -b <branch> |
| Discard changes to a file | git restore <file> | git checkout -- <file> |
| Command scope | Clearly limited to branch switching/creation | Overloaded: branches AND file restoration |
git switch / git checkout: Command Syntax and Examples
# Switch to an existing branch (modern)
git switch feature/login
# Create a new branch and switch to it in one step (modern)
git switch -c feature/dark-mode
# Equivalent legacy syntax
git checkout feature/login
git checkout -b feature/dark-mode
# Check your current branch after switching
git branch
# * feature/dark-mode
# feature/login
# main
# Attempting to switch with conflicting uncommitted changes (Git will refuse and warn)
git switch main
# error: Your local changes to the following files would be overwritten by checkout: ...
Breaking Down the git switch / git checkout Example
`git switch feature/login` and its legacy equivalent `git checkout feature/login` both move HEAD and update the working directory to match that branch. `git switch -c feature/dark-mode` (or `git checkout -b feature/dark-mode`) creates the branch and switches to it together. The `git branch` listing afterward confirms the asterisk has moved to the newly created branch. The final example shows Git's protective behavior: if uncommitted changes would be overwritten by the target branch's files, Git refuses the switch outright rather than silently discarding that work, prompting you to commit, stash, or discard those changes deliberately first.
How git switch / git checkout Is Used on Real Engineering Teams
- Modern onboarding documentation and Git tutorials increasingly teach git switch as the default, since it avoids the historical ambiguity and occasional accidental data loss associated with git checkout's overloaded behavior.
- Large, established codebases and their internal documentation frequently still use git checkout throughout, since it predates git switch and remains fully functional — developers on these teams need to be comfortable with both.
- IDE integrations (VS Code, IntelliJ) that provide a 'switch branch' UI button internally often still call the underlying checkout mechanism, regardless of which command name a developer would type manually.
- Automated deployment scripts frequently use git checkout <branch-or-tag> to move a build server to a specific version before running a build, a use case predating git switch's introduction.
git switch / git checkout Interview Questions and Answers
Q1. What is the difference between git switch and git checkout for changing branches?
For switching or creating branches, they accomplish exactly the same result. git switch is the modern command introduced in Git 2.23, scoped exclusively to branch operations. git checkout is the older, more overloaded command that handles both branch switching and discarding file changes, which historically caused some confusion and occasional accidental mistakes.
Q2. How would you create a new branch and switch to it in a single command?
Use git switch -c <branch-name> (modern) or the equivalent legacy syntax, git checkout -b <branch-name>. Both create the new branch at the current commit and immediately switch your HEAD and working directory onto it.
Q3. What happens if you try to switch branches while you have uncommitted changes that would be overwritten?
Git refuses the switch and displays an error message explaining that your local changes would be overwritten, rather than silently discarding them. You would need to commit, stash, or explicitly discard those changes before the switch can proceed.
git switch / git checkout Quiz: Test Your Understanding
1. Which modern command is used specifically for switching between branches?
- git restore
- git switch
- git rm
- git archive
Answer: B. git switch
Explanation: git switch, introduced in Git 2.23, is scoped exclusively to switching (and creating) branches, unlike the older, more overloaded git checkout.
2. Which flag creates a new branch and switches to it in one step using git switch?
- -a
- -d
- -c
- -m
Answer: C. -c
Explanation: The -c (create) flag with git switch creates a new branch at the current commit and immediately switches to it, equivalent to the legacy git checkout -b.
3. Why was git switch introduced when git checkout could already switch branches?
- git checkout was deprecated and no longer works
- git checkout was overloaded, also being used to discard file changes, which caused confusion
- git switch is faster at a technical level
- git checkout only worked on remote branches
Answer: B. git checkout was overloaded, also being used to discard file changes, which caused confusion
Explanation: git checkout historically served two very different purposes — switching branches and discarding file changes — which led to ambiguity and occasional mistakes; git switch was introduced to give branch operations their own clearly scoped, unambiguous command.
git switch / git checkout: Common Mistakes Beginners Make
- Assuming git checkout has been deprecated or removed — it remains fully functional and extremely common, especially in older codebases and tutorials.
- Forgetting the -c (or legacy -b) flag and running git branch plus a separate switch command instead of the more convenient combined step.
- Ignoring Git's refusal-to-switch warning about uncommitted changes and forcing the switch without understanding what work might be affected.
- Confusing git switch (for branches) with git restore (for file changes) — remembering that both replaced different aspects of the older overloaded git checkout.
git switch / git checkout: Exam-Ready Quick Notes
- git switch <branch> (modern) = git checkout <branch> (legacy): switch to an existing branch.
- git switch -c <branch> (modern) = git checkout -b <branch> (legacy): create and switch in one step.
- git switch was introduced in Git 2.23 to give branch-switching its own unambiguous command, separate from file-restoration.
- Git refuses to switch branches if uncommitted changes would be overwritten, protecting against silent data loss.
git switch / git checkout: Key Takeaways
- git switch is the modern, clearly scoped command for moving between branches, while git checkout remains a fully functional legacy alternative for the same purpose.
- The -c (or legacy -b) flag conveniently combines creating a new branch with switching to it in a single command.
- Git actively protects you from losing uncommitted work by refusing a branch switch that would overwrite conflicting changes.
Frequently Asked Questions About git switch / git checkout
Q1. What is the difference between git switch and git checkout?
For switching between branches, they do exactly the same thing. git switch is the newer, more clearly scoped command introduced in 2019 specifically for branch operations, while git checkout is the older command that also handles other tasks like discarding file changes, which git switch does not do.
Q2. How do I create a new branch and switch to it immediately?
Use git switch -c <branch-name> (modern) or git checkout -b <branch-name> (legacy). Both create the branch at your current commit and move you onto it in a single step.
Q3. Is git checkout outdated or going away?
No. It remains fully functional and extremely widely used, especially in older documentation, tutorials, and existing scripts. Git switch is the newer recommended option for branch operations specifically, but checkout isn't deprecated.
Q4. What happens if I try to switch branches while I have unsaved changes?
Git will refuse the switch if those changes would be overwritten by the target branch's files, displaying a clear warning rather than silently discarding your work. You'll need to commit, stash, or discard those changes first.
Q5. Why should I learn both git switch and git checkout?
Because you'll frequently encounter both in real projects — modern tutorials and teams may prefer git switch, but a huge amount of existing documentation, scripts, and colleagues will still use git checkout, so fluency with both is genuinely useful.
Summary
`git switch <branch>` is the modern command for moving your HEAD and working directory onto an existing branch, with `git switch -c <branch>` creating and switching to a new branch in one step. The legacy equivalents, `git checkout <branch>` and `git checkout -b <branch>`, accomplish exactly the same results and remain fully supported and extremely common in real-world use, since `git switch` was only introduced in Git 2.23 (2019) to give branch operations a clearer, unambiguous command separate from `git checkout`'s older, overloaded role in also discarding file changes. In either case, Git protects you by refusing to switch branches if doing so would silently overwrite uncommitted changes.