The git switch command is used to move from one Git branch to another. It changes the current branch and updates the working directory to match the selected branch.
git switch when you want to
change branches. Use git switch -c when you want to
create a new branch and switch to it in one step.
git switch is a Git command used to change the currently active branch.
git switch branch-name
For example:
git switch feature-login
Developers often need to move between different branches while working on a project.
The basic syntax is:
git switch branch-name
Replace branch-name with the branch you want to switch to.
Before switching, you can list local branches:
git branch
Example:
* main
feature-login
feature-dashboard
The asterisk indicates the current branch.
Suppose the repository contains a branch named
feature-login.
git switch feature-login
Git changes the current branch from main to
feature-login.
To return to the main branch:
git switch main
The working directory is updated to match the current state of
the main branch.
After switching branches, use:
git branch --show-current
For example:
feature-login
This confirms which branch is currently active.
You can also check the current branch using:
git status
Example:
On branch feature-login
nothing to commit, working tree clean
If the branch already exists, use:
git switch feature-dashboard
Git moves HEAD to the selected branch.
The -c option creates a new branch and switches to it.
git switch -c feature-payment
This is equivalent to creating a branch first and then switching to it.
| Command | Purpose |
|---|---|
git branch feature-login |
Creates a branch |
git switch feature-login |
Switches to an existing branch |
git switch -c feature-login |
Creates and switches to a branch |
If you have uncommitted changes, Git may allow the switch if those changes do not conflict with the target branch.
git status
git switch feature-login
Always check your working tree before switching branches.
Git can prevent switching if your local uncommitted changes would be overwritten by changes from the target branch.
Git may display a message similar to:
error: Your local changes to the following files would be overwritten
by checkout
In this situation, review your changes before continuing.
One option is to commit your current work:
git add .
git commit -m "Save current work"
git switch main
Your changes are now safely stored in a commit.
If your work is not ready for a commit, you can temporarily store it using Git stash.
git stash
git switch main
The stashed changes can later be restored.
Git can also work with remote-tracking branches such as
origin/main.
git switch --track origin/feature-login
This creates a local branch that tracks the specified remote-tracking branch when the branch does not already exist locally.
A local branch can track a remote branch. This makes commands such as pulling and pushing easier because Git knows the associated upstream branch.
git switch --track origin/feature-login
You can explicitly create a local branch from a remote-tracking branch:
git switch -c feature-login --track origin/feature-login
The local branch is created and configured to track the remote branch.
git switch feature-login
# Work on login
git switch feature-dashboard
# Work on dashboard
git switch feature-payment
# Work on payment
This allows you to move between independent areas of development.
When switching branches, Git updates tracked files in the working tree when the selected branch contains different committed versions of those files.
git switch maingit switch feature-loginThe working directory changes to represent the selected branch.
A simple and safe workflow is:
git status
git switch feature-login
git status
This lets you check the repository before and after the branch switch.
Before git switch was introduced, developers commonly
used git checkout to switch branches.
git checkout feature-login
Modern Git provides git switch as a clearer command
specifically for branch switching.
HEAD represents the currently checked-out branch in the normal branch workflow.
HEAD → main
After:
git switch feature-loginHEAD → feature-login
Before merging a feature branch into main, you normally
switch to the branch that should receive the changes.
git switch main
git merge feature-login
The merge operation will be covered in the next lessons.
You can switch to another branch to inspect or test its code.
git switch feature-loginAfter testing, return to the previous branch:
git switch main
git switch with git switch -c.git switch main
git switch feature-login
git switch -c feature-login
git switch --track origin/feature-login
git switch -c feature-login --track origin/feature-login
git branch --show-current
git status
# View branches
git branch
# Check current branch
git branch --show-current
# Check working tree
git status
# Switch to feature branch
git switch feature-login
# Check status again
git status
# Return to main
git switch main
git switch main
git switch -c feature-homepage
# Make homepage changes
git add .
git commit -m "Add homepage"
git switch main
git switch feature-homepage
git status
In this example, the developer creates a homepage branch, commits
the work, returns to main, and later switches back to
the feature branch.
The git switch command is used to move between branches.
git switch branch-name
To create and switch to a new branch:
git switch -c branch-name
Always check your working tree before switching, especially when you have uncommitted changes.
git switch changes the current branch.git switch branch-name switches to an existing branch.git switch -c branch-name creates and switches to a new branch.git branch can be used to view local branches.git branch --show-current shows the current branch.git status before switching branches.git switch --track can create a local tracking branch from a remote-tracking branch.git checkout was traditionally used for branch switching.Question: Which command is used to switch to an existing Git branch?