Tutorials  /  Linux Basics

Git Switch Command: How to Change and Create Branches

Ccentron Redaktion · September 2025 ·5 min read ·Linux Basics, Tutorial

Branch switching in Git means adjusting your working directory so that it matches the state of a chosen branch. When you move to another branch, Git aligns the files in your working directory with the snapshot of the target branch. This happens by moving the "HEAD" pointer, which marks the branch you are currently on. This setup lets you easily shift between different development contexts without losing your work. This guide explains how to use the git switch command for switching between local and remote branches.

The Short Answer Version

If you already know Git and only need a quick overview of branch switching commands, here they are:

Console
# Switch to an existing local branch
$ git switch <branch_name>

# Create and switch to a new branch
$ git switch -c new_branch_name

# Switch to a remote branch
$ git switch -c branch_name origin/branch_name

Continue reading for detailed explanations and usage examples.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Switch Branches in Git

This section gives an in-depth look at the git switch command.

Command Explanation

The git switch command (available starting with Git 2.23) changes your working directory so that it reflects the state of the target branch.

Command Syntax with Breakdown

The general syntax to switch to the main branch is:

Console
git switch <branch-name>

Explanation of parts:

  • git switch: The command that initiates the branch change.
  • <branch-name>: The branch you want to switch to.

Command Flags

You don’t need extra flags to switch to an existing branch like main. But here are useful ones:

  • -f or --force: Forces the switch and discards uncommitted changes (may cause data loss).
  • --merge: Attempts to merge current branch changes into the new branch.
  • --detach: Moves you into a detached HEAD state at the target commit.

Command Demonstration

Example of switching to the main branch:

Check which branch you are currently on:

Console
$ git branch

Sample output:

  feature-login
* hotfix-header
  main

Here, you’re on hotfix-header (marked with the asterisk).

Switch to the main branch:

Console
$ git switch main

Output:

Switched to branch 'main'
Your branch is up to date with 'origin/main'.

Confirm you are now on main:

Console
$ git branch

Output:

  feature-login
  hotfix-header
* main

The asterisk has moved, showing you are on the main branch.

Switch to a New Branch

Create and switch to a new branch with one command:

Console
$ git switch -c <new_branch_name>

The -c (or --create) flag creates and switches to a new branch. Other useful options:

  • -C or --force-create: Creates or resets the branch if it already exists.
  • --track: Sets up tracking info for the branch.
  • --no-track: Explicitly avoids setting up tracking info.

Switch to a Remote Branch

Remote branches exist on repositories hosted elsewhere and may need fetching before use. When collaborating, it’s common to switch to a remote branch by creating a local tracking branch that follows it.

First, update your local repository to include all remote branches:

Console
$ git fetch

Create a local branch that tracks the remote one and switch to it:

Console
$ git switch -c <branch_name> origin/<branch_name>

Explanation:

  • git switch: The command to change branches.
  • -c: Creates a new local branch.
  • <branch_name>: Your new local branch name (usually same as remote).
  • origin/<branch_name>: The remote reference.

For Git 2.23+, you can use a shorter version:

Console
$ git switch --track origin/<branch_name>

This automatically uses the remote branch name for the local one.

Other related flags:

  • --track or -t: Sets up branch tracking.
  • --no-track: Prevents tracking setup.
  • -c: Forces branch creation even if it exists.

Conclusion

You now know how to use git switch to move between branches in Git. You can jump to existing branches, create and switch to new ones, and connect to remote branches locally. These abilities are essential for keeping a smooth Git workflow and let you handle multiple tasks, features, or fixes effectively. For more advanced options, consult the Git documentation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren