
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git Switch
The git switch command is used to switch the branches in Git. It is a more modern command than git checkout and was introduced in Git 2.23 to simplify the process of branch switching.
Syntax
git switch <branch-name>
<branch-name> is the name of the branch to which you need to switch.
Options
The git switch command has the following options:
-c or --create
Using the command git switch -c creates a new branch and immediately switches to it.
git switch -c new-branch git switch -create new-branch
-C or --create --force
Using the command git switch -C creates a new branch and immediately switches to it, even if the branch already exists. It resets the branch and overwrites it.
git switch -C <branch-name>
-f or --force
It switches branches and discard uncommitted changes.
git switch -f <branch-name>
--detach
It switches to the given commit, but detaches the HEAD, which signifies that you are no longer on any branch.
This effectively means that you are working with a commit, without affecting any branch.
git switch --detach <commit-hash>
--guess
Using this option will let you provide a part of the branch name and Git in turn will guess the name and return the result, switching to that branch.
This option enabled by default, but can be turned off using the option --no-guess.
git switch --guess <branch-name-part>
--track
This option automatically sets the tracking up of a new branch from a remote branch.
This option is useful when you are checking out a branch that exists on a remote repository, and you need to set the local branch to track the remote branch.
git switch --track <remote-branch>
--no-track
When a branch is created from a remote-tracking branch, Git sets the tracking of the branch automatically.
In order to prevent the tracking of branch, --no-track option can be used.
git switch --no-track <branch-name>
--orphan
Creates a new branch with no history. Useful to create new branches with a clean slate.
git switch --orphan <new-branch-name>
--discard-changes
Similar to -f, it will also switch branches while discarding the changes that are uncommitted in the working directory.
It is useful when you want discard the changes and don't want to force the switch.
git switch --discard-changes <branch-name>
--merge
This option avoids losing changes in the working directory by trying to merge them.
git switch --merge <branch-name>
--progress and --no-progress
This option controls whether progress needs to be displayed while switching or not.
It is generally used in long operations that may show the progress, such as checking out large files.
git switch --progress <branch-name> git switch --no-progress <branch-name>
You can manage switching of branch with more precision and clarity with git switch as compared to the older commands like git checkout.