Graphite Reviewer is now Diamond

Understanding the Differences Between `git checkout` and `git switch`

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


If you're managing branches in Git, you've likely encountered git checkout and git switch. While both commands are used for branch management, their purposes and use cases differ. With newer Git versions, git switch has emerged as a simpler alternative for specific tasks. This guide explains their differences, usage, and benefits, including where the Graphite CLI fits in to optimize your workflow.

git checkout has been a versatile command, used for checking out branches and switching files in your working directory. However, its multi-purpose nature has led to confusion among developers. To address this, Git introduced two commands: git switch for branch operations and git restore for file operations.

By segregating these functionalities, Git aims to improve usability and reduce mistakes. While git checkout remains fully functional, adopting git switch offers a cleaner and more intuitive workflow for managing branches.

git switch is dedicated to switching branches. Its syntax and behavior are streamlined, making it easier to understand. Key features include:

  • Switching to an existing branch:

    Terminal
    git switch <branch_name>

    This command is equivalent to git checkout <branch_name> but avoids ambiguity.

  • Creating and switching to a new branch:

    Terminal
    git switch -c <new_branch_name>

    The -c flag creates the branch and checks it out in one step.

  • Switching branches without updating files: You can use options like --detach to checkout a branch in detached HEAD state.

git checkout retains its versatility, allowing it to:

  • Switch between branches:

    Terminal
    git checkout <branch_name>
  • Create a new branch and switch to it:

    Terminal
    git checkout -b <new_branch_name>
  • Restore files or paths from a commit:

    Terminal
    git checkout <commit_hash> -- <file_path>

This multi-functionality can be both a strength and a drawback, as it combines branch management and file restoration in one command.

The Graphite CLI takes Git commands like checkout and switch to the next level, especially when working with stacked pull requests. Here's an example from the Graphite CLI workflow:

  • Switching to a branch using Graphite CLI:
    Terminal
    gt checkout main

This simplifies the process by focusing on stacking and syncing changes. For instance, Graphite enables creating a new stack with gt create and syncing branches with gt sync, which internally optimizes Git workflows.

Featuregit checkoutgit switch
Branch switchingYesYes
Branch creationYes (-b flag)Yes (-c flag)
File restorationYesNo
UsabilityMore complex due to multi-functionalityFocused and user-friendly

While git checkout remains versatile, git switch offers:

  • Simplicity: Focused on branch management, avoiding accidental file operations.
  • Clarity: Streamlined syntax that reduces confusion for new users.
  • Consistency: Works well with modern Git workflows.

For most branch operations, git switch provides a cleaner and more focused experience. However, git checkout remains a powerful tool for advanced use cases involving file restoration. Incorporating tools like Graphite CLI can further enhance your Git workflow by simplifying branch and pull request management, making it a strong addition to your development toolkit.

For more details on the Graphite CLI and its features, check out the quick start guide.

Built for the world's fastest engineering teams, now available for everyone