git switch
git switch
: switch between branches.
The git switch
command allows you to navigate between branches in your repository. When you switch to a branch, Git updates your working directory to match the state of that branch, allowing you to work on a different version of your project without affecting other branches.
With the -c
or --create
flag, you can create a new branch and switch to it in a single command.
git switch
is a relatively new command introduced in Git 2.23 to simplify the process of switching between branches. Before that, developers had to use the multipurpose git checkout
command for both switching branches and other tasks like discarding changes in the repository. This was a source of confusion for newcomers. The git switch
command simplifies the workflow and reduces the potential for mistakes by providing a dedicated command for branch switching.
Examples
Switch to an existing branch named login-feature
:
git switch login-feature
Create a new branch named login-form-bugfix
and switch to it:
git switch -c login-form-bugfix
Switch back to the previous branch:
git switch -
Switch to a specific commit, entering a detached HEAD state (meaning you're not on a branch):
git switch --detach a1b2c3d
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.