LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

git-checkout

Switch branches or restore working tree files

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 with -b, file restoration with --, and detached HEAD operations. Since Git 2.23, the recommended approach is git switch for branches and git restore for files, which provide clearer separation of concerns.Despite the newer alternatives, git checkout remains widely used for its versatility and is deeply embedded in existing workflows, documentation, and scripts.

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.
-d, --detach
Detach HEAD at the named commit instead of updating a branch.
-p, --patch
Interactively select hunks to restore from the index or tree.
-m, --merge
Perform a 3-way merge between the current branch, your local changes, and the new branch.
--orphan BRANCH
Create orphan branch.
--help
Display help information.

INSTALL

sudo apt install git
copy
sudo dnf install git
copy
sudo pacman -S git
copy
sudo apk add git
copy
sudo zypper install git
copy
brew install git
copy
nix profile install nixpkgs#git
copy

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

RESOURCES

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid