LinuxCommandLibrary

git-checkout

TLDR

Switch to branch

$ git checkout [branch-name]
copy
Create and switch to branch
$ git checkout -b [new-branch]
copy
Restore a file
$ git checkout -- [file.txt]
copy
Checkout specific commit
$ git checkout [commit-hash]
copy
Checkout from remote
$ git checkout -t origin/[branch]
copy

SYNOPSIS

git checkout [options] branch|commit|file

DESCRIPTION

git checkout switches branches or restores files. It updates the working tree to match the specified branch, commit, or file version from history.
The command serves multiple purposes: branch switching, branch creation, file restoration, and detached HEAD operations. Modern git prefers git switch for branches and git restore for files.
git checkout remains widely used for its versatility.

PARAMETERS

BRANCH

Branch to switch to.
-b BRANCH
Create and switch to new branch.
-t, --track
Set up tracking for remote branch.
-- FILE
Restore file from index.
-f, --force
Force switch, discard changes.
--orphan BRANCH
Create orphan branch.
--help
Display help information.

CAVEATS

Uncommitted changes may be lost with -f. Detached HEAD requires care. Consider git switch/restore for clarity.

HISTORY

git checkout is an original Git command that combines several operations. Git 2.23 introduced git switch and git restore as clearer alternatives.

SEE ALSO

Copied to clipboard