By admin , 29 April 2026

Synopsis

git checkout [<branch>]
git checkout -b <new-branch> [<start-point>]
git checkout [<tree-ish>] -- <file>...

Description

The git checkout command historically does two unrelated things: switching branches and restoring files. Because of this overload, modern Git introduced git switch for branch operations and git restore for file operations. git checkout still works and remains popular, but new users are encouraged to learn the newer split commands.

When given a branch name, git checkout moves HEAD to that branch and updates the working tree. When given a path, it overwrites the working-tree file with the version from the index (or from a specified commit). The -b flag creates a new branch and switches to it in one step.

In day-to-day use, git checkout integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git checkout --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git checkout interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git checkout's side effects helps avoid all three.

Common Options

OptionDescription
-b <name>Create and switch to a new branch.
-B <name>Create or reset a branch and switch to it.
--trackSet up tracking for a remote branch.
--detachCheck out a commit in detached HEAD state.
-- <path>Treat following arguments as paths, not branches.
-f, --forceDiscard local changes when switching.
-p, --patchInteractively select hunks to restore.

Examples

git checkout main
# Switch to the main branch

git checkout -b feature/api origin/develop
# Create a new branch tracking origin/develop

git checkout HEAD -- src/config.js
# Discard local changes to a single file

git checkout v1.2.3
# Detached HEAD at a specific tag

Common Mistakes

Running git checkout <file> silently overwrites your local edits with no undo. Use git restore instead — it has a clearer name. Detached HEAD state surprises beginners: commits made there belong to no branch and may be lost when you switch away. If you want to keep them, create a branch first.

Related Commands

git switch, git restore, git branch, git reset