Hotfix interruption
You're partway through a feature when an urgent fix comes in. You create a branch with git switch -c hotfix main, fix the bug, and return to the feature with git switch -.
Git · Command
git switch moves you onto a different branch. It can also create a branch, reset one, detach HEAD at a commit, or start an orphan branch as part of the same command.
Last updated
When you run git switch <branch>, Git points HEAD at that branch. It then rewrites the index and working tree so they match the branch's tip, and every commit you make after that goes on top of the branch. You can add flags to create a branch first (-c), reset an existing one (-C), or land on a bare commit with no branch (--detach). Unlike git checkout, switch never restores individual files and only changes branches. If the switch would overwrite your uncommitted work, Git stops unless you tell it to throw that work away or carry it along.
git switch [<options>] [--no-guess] <branch>
git switch [<options>] --detach [<start-point>]
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --orphan <new-branch>| Name | Type | Description |
|---|---|---|
<branch> | branch name | The existing branch to move to. Use - for the previous branch, or @{-N} for the Nth-last branch or commit you switched to. |
<new-branch> | branch name | The name to give the branch created by -c, -C or --orphan. |
<start-point> | commit-ish | Where the new branch (or detached HEAD) begins. It defaults to the current HEAD. You can write A...B to mean their single merge base. |
-c, --create | flag | Create <new-branch> at <start-point> and then switch to it. |
-C, --force-create | flag | Same as -c, but if the branch already exists it is reset to <start-point>. |
-d, --detach | flag | Switch to a commit without being on any branch. |
--guess / --no-guess | flag | Turns on or off creating a tracking branch automatically from a remote branch with the same name. It is on by default, and the checkout.guess setting can change that. |
-f, --discard-changes | flag | Switch even when you have local changes, and throw those changes away. |
-m, --merge | flag | Stash conflicting local changes automatically and reapply them after the switch. |
--conflict=<style> | merge | diff3 | zdiff3 | Works like --merge but sets how conflict hunks are shown. |
-t, --track[=(direct|inherit)] / --no-track | flag | Turns upstream configuration for the new branch on or off. -t implies -c. |
--orphan | flag | Create a new branch that has no commits yet. |
# Start a feature branch from main
git switch -c feature/login main
# Carry uncommitted edits to another branch
git switch -m hotfix
# Jump back to the previous branch
git switch -
# Inspect an old commit without a branch
git switch --detach HEAD~3
# Keep experimental work done while detached
git switch -c spike/idea
# Track a remote branch (auto-guessed from origin)
git switch new-topicThese commands show the common ways to use git switch: creating a branch, carrying local edits, jumping back, detaching HEAD, keeping detached work, and tracking a remote branch automatically.
You're partway through a feature when an urgent fix comes in. You create a branch with git switch -c hotfix main, fix the bug, and return to the feature with git switch -.
After git fetch, running git switch their-topic creates a local branch that tracks origin/their-topic, so you don't need to type any --track options.
To check whether an old release had a bug, you run git switch --detach v1.2.0 and build or test it without moving any branch.
git switch --orphan gh-pages starts an empty branch with no shared history, for example to hold generated site files.
git checkout does two unrelated jobs: it changes branches and it restores files, so a slip in its arguments can overwrite your work. git switch only changes branches, and git restore handles files. That split makes it clearer what each command is meant to do and harder to destroy changes by accident.
Follow-up: What is the git switch equivalent of git checkout -b feature origin/main? Answer: git switch -c feature origin/main. Depending on branch.autoSetupMerge, it may also set up upstream tracking to origin/main.
Git switches and keeps your changes as long as none of the files you edited differ between the two branches. If a file does differ, Git refuses and reports that you have local changes. You can then use -m to stash the conflicting edits and reapply them after the switch, or -f/--discard-changes to throw them away.
Follow-up: After git switch -m reports conflicts, how do you clean up? Answer: the stash entry is kept, so you resolve the conflicts and then run git stash drop. Alternatively, reset the working tree and run git stash pop later.
-c creates a new branch and fails if that name already exists. -C creates it, or if it already exists, resets it to the start point, which is the same as git branch -f followed by git switch. Both are transactional, so nothing changes if the switch itself fails.
Follow-up: Why can git switch refuse a branch even though it exists? Answer: another worktree may already have it checked out. You can override that with --ignore-other-worktrees, which lets more than one worktree hold the same branch.
git checkoutgit branchgit restoregit stashgit worktreegit mergegit config