
In this post, we will look at one of the sub-commands that are part of git, the git switch command. Before we get into things, though, let me clarify a few things. Git is a distributed version control system primarily used for tracking changes in source code during software development. A branch is a separate line of development within a repository. The switch sub-command which is what we’re looking at today is used to switch to a specified branch of development.
Basic Usage
Basic usage of the git switch command is as follows:
git switch [<options>] [<branch>]
We start with git switch, followed by any options, and then the branch name.
Flags
Below are my picks from the commands help text, for an exhaustive list please refer to the man page or git’s website.
-c, –create
create and switch to a new branch
–discard-changes
discard away local modifications
-q, –quiet
suppress progress reporting
–progress
force progress reporting
-m, –merge
perform a 3-way merge with the new branch
–conflict <style>
conflict style (merge, diff3, or zdiff3)
-f, –force
force checkout (throw away local modifications
Examples
Below are some examples I feel are the most useful.
Switching To An Existing Branch
To switch to an existing branch, use the following command:
git switch <branch-name>
This command changes the context of your work to the specified branch, allowing you to work with the files and commit history of that branch.
Create A New Branch From A Different Branch
You can create a new branch based on a different branch as follows:
git switch remotes/origin/example -c hello-world
This command switches to the remote branch name example from the remote origin and creates a new branch from that branch.
Switch To The Previous Branch
To switch to the previous branch, use the following command:
git switch -
This command is a short way to switch back to the branch you were working on before the current one.
Switch To A Branch And Automatically Merge The Current Branch (Including Uncommitted Changes)
To merge while you switch branches, try this command:
git switch --merge branch-name
This command switches to a specified branch while merging any changes from your current branch.
Conclusion
In this post, we looked at one of the many sub-commands which are part of git, the git switch command. We covered basic usage, flags of the command, and examples. Proving that the switch sub-command can be used for more than just switching to a different local branch, such as to checkout remote branches, and to create a new branch.
Looking For More
Are you looking for more and a place to start? If so, why not check out our other gits posts?

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply