Switching branches should be boring. It stops being boring when the branch only exists on the remote, your working tree is already dirty, or old examples keep pointing you back to git checkout. To switch a Git branch cleanly, use git switch for the branch move itself and confirm what Git is tracking before you keep working.
Modern Git split the old git checkout behavior into narrower commands for a reason. git switch handles branch changes, while git restore handles file content recovery. The examples below stay on the branch moves people actually need: switching to an existing branch, creating a new one, following a remote branch, jumping back to the previous branch, and fixing the errors that usually interrupt the workflow.
Git introduced
git switchandgit restorein version 2.23 to split branch changes away from the older all-purpose git-checkout workflow. If your installed Git build is older than that, use the legacygit checkoutequivalents until you update.
Understand the Git Switch Command
To switch Git branches quickly, use git switch <branch> for an existing local branch, git switch -c <new-branch> to create and switch in one step, git switch <branch> or git switch --track origin/<branch> for a remote-tracking branch, and git switch - to jump back to the previous branch.
Git Switch Branch Syntax
The main reason git switch feels cleaner than git checkout is scope. It only deals with branch or commit movement, so you do not have to remember whether the same command might also overwrite files in your working tree. Use the syntax forms below as the core patterns.
git switch <existing-branch>
git switch -c <new-branch>
git switch <remote-branch-name>
git switch --track origin/<remote-branch-name>
git switch -
git switch --detach <commit>
The official git-switch manual page documents the -c, --track, and --detach forms used in this guide, while the git-branch manual page covers the listing and tracking concepts around them. In practice, -c means “create the branch first” and --track means “tie the new local branch to a remote branch automatically.”
Git Switch vs Git Checkout
For branch work on modern Git, prefer git switch because the intent is narrower and the command is easier to read later in shell history. Keep git checkout in mind only for older Git builds or older team docs that still use forms such as git checkout feature/login-form and git checkout -b hotfix/session-timeout.
Git Switch Branch Quick Reference
| Task | Command | What It Does |
|---|---|---|
| Switch to an existing local branch | git switch feature/login-form | Moves your working tree to a branch that already exists locally. |
| Create and switch to a new branch | git switch -c hotfix/session-timeout | Creates the branch and checks it out in one step. |
| Switch to a remote branch by name | git switch feature/api-cleanup | Creates a local tracking branch when Git can match one remote branch name cleanly. |
| Switch to a remote branch with explicit tracking | git switch --track origin/feature/api-cleanup | Creates the local branch and ties it to the named remote branch explicitly. |
| Jump back to the previous branch | git switch - | Returns to the branch you were on right before the current one. |
| Inspect an older commit without moving a branch | git switch --detach HEAD~1 | Detaches HEAD at a commit for inspection or testing. |
| Use the legacy checkout equivalent | git checkout feature/login-form | Provides the same branch switch on older Git builds that do not include git switch. |
Install Git and Verify the Git Switch Command
Git already ships with many developer desktops and build environments, but minimal servers, stripped containers, and older installs can still leave you without the command you need. If git is missing, or the installed version is too old for git switch, install or update Git and rerun the version check.
Install Git on Debian and Ubuntu
sudo apt update && sudo apt install git
Install Git on Fedora, RHEL, Rocky Linux, and AlmaLinux
sudo dnf install git
Install Git on Arch Linux
sudo pacman -S git
Install Git on openSUSE
sudo zypper install git
Verify that Git is available:
git --version
git version 2.x.x
git switch arrived in Git 2.23. If your version is older than that, update Git or use the older git checkout <branch> and git checkout -b <new-branch> forms until you can move to a newer release.
If you want distro-specific packaging steps, use the guides to Install Git on Ubuntu, Install Git on Debian, Install Git on Fedora, Install Git on Linux Mint, Install Git on Arch Linux, or Install Git on Rocky Linux.
If this is a brand-new Git setup and your first commit still complains about author identity, configure Git username and email before you start creating or moving branches in disposable test repositories.
Use Git Switch in a Local Repository
Most branch changes stay local. Start by checking what already exists in the repository, then switch or create the branch you need.
List Local and Remote Git Branches Before Switching
The quickest check is git branch -a. It shows local branches first and remote-tracking branches underneath, which makes it easier to tell whether a branch is already local or still only on the remote.
git branch -a
feature/api-cleanup feature/login-form feature/release-notes * main remotes/origin/feature/api-cleanup remotes/origin/feature/login-form remotes/origin/main
The asterisk marks your current branch. Entries prefixed with remotes/origin/ are the remote-tracking branch entries Git keeps for that server, not editable local branches yet.
Switch to an Existing Local Git Branch
To switch to an existing Git branch, pass the local branch name directly. This is the basic git switch workflow most people use dozens of times a day.
git switch feature/release-notes
git status --short --branch
Switched to branch 'feature/release-notes' ## feature/release-notes
The status line confirms the branch name immediately. In this example there is no upstream branch configured yet, so the output only shows the local branch.
Create and Switch to a New Git Branch
When the branch does not exist yet, add -c. That combines branch creation and switching into one command, which is cleaner than running git branch first and git switch second.
git switch -c hotfix/session-timeout
git status --short --branch
Switched to a new branch 'hotfix/session-timeout' ## hotfix/session-timeout
The new branch starts from your current HEAD, so make sure you are already on the correct base branch before you create it. This is the cleanest way to split a hotfix, feature spike, or review fix away from the branch you are already using.
Use Git Switch for Remote Branches
Remote branch switching is where the small Git details matter more. If a teammate created the branch on the server, fetch first if it does not appear in git branch -a, then either let Git guess the tracking branch by name or set the tracking relationship explicitly.
Switch to a Remote Git Branch by Name
If one remote-tracking branch matches the name you typed, Git can create the local branch and attach it to the remote automatically.
git switch feature/api-cleanup
git status --short --branch
Switched to a new branch 'feature/api-cleanup' branch 'feature/api-cleanup' set up to track 'origin/feature/api-cleanup'. ## feature/api-cleanup...origin/feature/api-cleanup
This works because Git can guess one clean match from the remote-tracking branch entries it already knows about. If you use multiple remotes or the branch name is ambiguous, switch with an explicit remote name instead.
Switch to a Remote Git Branch with Explicit Tracking
Use --track when you want to be explicit about which remote branch the new local branch should follow. This is the safer habit when repositories have both origin and upstream, or when two remotes expose the same branch name.
git switch --track origin/feature/api-cleanup
Switched to a new branch 'feature/api-cleanup' branch 'feature/api-cleanup' set up to track 'origin/feature/api-cleanup'.
Replace origin with the actual remote name when needed. The local branch name does not have to match perfectly, but keeping it the same is usually the least confusing option.
Use Advanced Git Switch Branch Workflows
Once the basic moves are automatic, two smaller git switch patterns save time regularly: jumping back to the branch you just left and detaching HEAD when you need to inspect an older commit without renaming or rewriting anything.
Switch Back to the Previous Git Branch
The lone dash is the quickest way to bounce between two branches during review, debugging, or cherry-pick prep.
git switch -
git status --short --branch
Switched to branch 'main' Your branch is up to date with 'origin/main'. ## main...origin/main
This is especially useful when you keep bouncing between main and one feature branch during review, testing, or a quick cherry-pick.
Inspect an Older Commit Without Moving a Git Branch
If you need to inspect a commit instead of a branch, detach HEAD on purpose. This lets you review or test an older snapshot without moving any branch pointer.
git switch --detach HEAD~1
git status --short --branch
HEAD is now at f3b242d Initial commit ## HEAD (no branch)
When you are done, switch back to a real branch such as main. If you decide the detached state needs follow-up commits, create a branch immediately with git switch -c review-old-state so the work does not stay orphaned.
Troubleshoot Common Git Switch Errors
Git Says the Branch Reference Is Invalid
fatal: invalid reference: feature/payments-ui
This usually means the branch name is wrong, the branch only exists on the remote and you have not fetched it yet, or the branch does not exist at all. Refresh the remote refs, list what Git can actually see, and then switch using the real branch name.
git fetch --prune origin
git branch -a
git switch feature/api-cleanup
feature/login-form * main remotes/origin/feature/api-cleanup remotes/origin/feature/login-form remotes/origin/main Switched to a new branch 'feature/api-cleanup' branch 'feature/api-cleanup' set up to track 'origin/feature/api-cleanup'.
If the branch still does not appear after git fetch --prune origin, the name is wrong or the branch does not exist on that remote.
Git Refuses to Switch Because Local Changes Would Be Overwritten
error: Your local changes to the following files would be overwritten by checkout: README.md Please commit your changes or stash them before you switch branches. Aborting
This happens when the target branch would replace a file you already modified locally. The safest fix is to stash the work, switch branches, and then decide whether to reapply the stash there or return later.
git status --short --branch
git stash push -u -m "readme-switch-work"
git switch feature/login-form
git stash list
## main
M README.md
Saved working directory and index state On main: readme-switch-work
Switched to branch 'feature/login-form'
stash@{0}: On main: readme-switch-work
The stash entry confirms the work is still available. When you want it back, use git stash pop on the branch that should receive the changes.
Git Expects a Branch but You Passed a Commit
fatal: a branch is expected, got commit 'HEAD~1' hint: If you want to detach HEAD at the commit, try again with the --detach option.
This error appears when you ask git switch to move to a raw commit as though it were a branch name. Use detached HEAD mode when the target is a commit hash, a tag, or a relative reference such as HEAD~1.
git switch --detach HEAD~1
git status --short --branch
HEAD is now at f3b242d Initial commit ## HEAD (no branch)
If the detached state turns into real work, create a branch before committing so that the new commits stay attached to a named branch.
Frequently Asked Questions About the Git Switch Command
Use git switch followed by the local branch name, such as git switch feature/login-form. If the branch already exists locally, Git moves your working tree to that branch immediately.
git switch and git checkout?
git switch is the newer branch-focused command, while git checkout is the older multi-purpose command that also handles file restoration. For branch changes on modern Git, git switch is usually clearer and harder to misuse.
If Git can match the remote branch name cleanly, git switch feature/api-cleanup creates a local tracking branch automatically. To be explicit, use git switch --track origin/feature/api-cleanup.
git switch create a new branch?
Yes. Use git switch -c new-branch-name to create the branch and switch to it in one command.
Run git switch - to jump back to the branch you had checked out right before the current one.
Sometimes. If the target branch would not overwrite those files, Git switches normally. If it would overwrite them, Git aborts and asks you to commit or stash the changes first.
Git Switch Branch Conclusion
Git branch changes now come down to a few predictable moves: switch to an existing branch, create one with -c, attach cleanly to a remote branch, or detach HEAD when you only need an older snapshot for a moment. If the branch name needs cleanup next, rename a local and remote Git branch or undo the last Git commit after you switch back to the branch you want to keep.
Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>