git-switch - Linux


Overview

git-switch allows you to quickly and efficiently change between tracked branches, revisions, or stashes in a Git repository. It offers an intuitive and concise syntax, empowering you to navigate your project’s history with ease.

Syntax

git switch [-q] [-C <commit-ish>] [-c] [-f] <branch-name>
git switch [-q] [-C <commit-ish>] [-c] [-f] <stash-ref>

Parameters

<branch-name> | <stash-ref>
The target branch or stash to switch to.

Options/Flags

  • -q: Quiet mode, suppresses informational messages.
  • -C <commit-ish>: Change to a new branch starting from a specific commit-ish (e.g., branch name, tag, or commit hash).
  • -c: Create a new branch if it doesn’t exist.
  • -f: Force checkout, discarding any uncommitted changes. Use with caution.

Examples

Switching to a Branch

git switch main

Switching to a Stash

git switch stash@{0}

Switching to a Specific Commit and Creating a New Branch

git switch -c -C 123456 my-new-branch

Force Checkout with Uncommitted Changes

Caution: Use -f only if necessary. It can lead to data loss.

git switch -f my-branch

Common Issues

  • Conflict with uncommitted changes: If you switch to a branch with conflicting changes, you’ll need to resolve them before switching back.
  • Non-existent target: Using non-existent branch names or stash references will result in an error.

Integration

  • git-checkout: Use git-switch as a quick alias for git-checkout -b.
  • git-stash: Quickly switch to a stashed state and back using git-switch.

Related Commands

  • git-branch: List and manage branches.
  • git-add: Stage changes for commit.
  • git-stash: Save and restore uncommitted changes.