git-checkout
Switch branches or restore working tree files
TLDR
Create and switch to a new branch
Create and switch to a new branch based on a specific reference (branch, remote/branch, tag are examples of valid references)
Switch to an existing local branch
Switch to the previously checked out branch
Switch to an existing remote branch
Discard all unstaged changes in the current directory (see git reset for more undo-like commands)
Discard unstaged changes to a given file
Replace a file in the current directory with the version of it committed in a given branch
SYNOPSIS
git checkout [options] [{-b | -B | --orphan} <new-branch>] [<branch>] [--] [<pathspec>…]
PARAMETERS
-b
Create new branch <name> from current HEAD and switch to it
-B
Create or reset existing branch <name> to current HEAD and switch
--detach [
Detach HEAD to specified <commit> or current HEAD
-f, --force
Overwrite uncommitted local changes in worktree
-m, --merge
Perform three-way merge for files with conflicts
-p, --patch
Interactively select hunks to checkout via patch mode
-q, --quiet
Suppress progress reporting and warnings
--track, -t
Set upstream tracking for new branch
--orphan
Create new orphan branch with empty history
-l
Create linked checkout for worktree (lightweight)
--ours, --theirs, -2
Override conflict style: ours/theirs or octopus
--conflict=
Set merge conflict style (diff3, merge, etc.)
--ignore-other-worktrees
Ignore other worktrees when checking out branch
--overwrite-ignore
Update ignored files in worktree
--progress
Force progress display even in quiet mode
DESCRIPTION
git checkout is a multi-purpose Git command for switching branches, detaching HEAD, or restoring specific files/paths in the working tree and index. When invoked with a branch name, it updates HEAD to point to that branch's tip, resetting the index and worktree accordingly. Using -b or -B creates new branches. With --detach, it points HEAD directly at a commit, useful for inspecting history without branch changes.
For paths, it checks out files from the index, HEAD, or a specified tree-ish into the worktree, overwriting local changes unless -p (patch mode) is used for interactive selection. Options like -m enable merging conflicted files.
Warning: Without -f, it refuses to overwrite uncommitted modifications, protecting data. It's powerful but risky for data loss. Since Git 2.23 (2019), git switch is preferred for branch switching and git restore for file checkouts, clarifying git checkout's overloaded semantics and reducing errors.
CAVEATS
Risks data loss by overwriting uncommitted changes (use git status first). Branch switching discouraged since Git 2.23—prefer git switch. Detached HEAD forgets branch context unless noted.
EXAMPLES
Switch branch: git checkout main
New branch: git checkout -b feature develop
Restore file: git checkout HEAD -- file.txt
Detach: git checkout --detach v1.0
STATUS CHECK
Always run git status before checkout to avoid losing uncommitted work.
HISTORY
Introduced in Git 1.5.0 (2007) as core porcelain command. Evolved with worktree support (1.8.0, 2013) and safety options. In Git 2.23 (2019), split into git switch/git restore for clarity; git checkout remains but marked legacy.
SEE ALSO
git-switch(1), git-restore(1), git-branch(1), git-worktree(1)


