LinuxCommandLibrary

git-checkout

Switch branches or restore working tree files

TLDR

Create and switch to a new branch

$ git checkout -b [branch_name]
copy

Create and switch to a new branch based on a specific reference (branch, remote/branch, tag are examples of valid references)
$ git checkout -b [branch_name] [reference]
copy

Switch to an existing local branch
$ git checkout [branch_name]
copy

Switch to the previously checked out branch
$ git checkout -
copy

Switch to an existing remote branch
$ git checkout [[-t|--track]] [remote_name]/[branch_name]
copy

Discard all unstaged changes in the current directory (see git reset for more undo-like commands)
$ git checkout .
copy

Discard unstaged changes to a given file
$ git checkout [path/to/file]
copy

Replace a file in the current directory with the version of it committed in a given branch
$ git checkout [branch_name] -- [path/to/file]
copy

SYNOPSIS

git checkout [options] [{-b | -B | --orphan} <new-branch>] [<branch>] [--] [<pathspec>…]

PARAMETERS

-b , --branch
    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=