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 [-q] [-f] [-m] [--detach] [-b |-B ] []
git checkout [-q] [-f] [-m] [--detach] [] [--] ...

PARAMETERS

-q
    Be quiet, suppress feedback messages.

-f, --force
    Proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.

-m, --merge
    Perform a three-way merge to incorporate changes into your working tree.

--detach
    Operate in detached HEAD state, rather than switching to a named branch.

-b
    Create a new branch named and switch to it.

-B
    If exists, reset it to .


    The branch to switch to.


    Checkout files as they existed in .

...
    The files to restore.

DESCRIPTION

The git checkout command is a versatile tool in Git used to switch between different branches or restore working tree files. When used with a branch name, it updates the files in your working directory to match the version stored in that branch, effectively changing the current branch you're working on. When used with a file path, it restores the specified file(s) in your working directory to their state in the index (staging area) or a specified commit. Be careful when using git checkout, as changes in your working directory might be overwritten during branch switching if not properly committed or staged. It's a fundamental command for navigating Git repositories, collaborating on projects, and undoing changes.

CAVEATS

Uncommitted changes can be lost if switching branches without committing or stashing them first. Use git stash to save changes temporarily.

DETACHED HEAD

Checking out a commit directly (e.g., git checkout ) puts you in a "detached HEAD" state. This means you're not on any branch, and any new commits you make will not be part of any branch unless you explicitly create a new branch. It's useful for exploring historical states of the project.

RESTORING FILES

When used with file paths, git checkout restores those files to a specific state.
For example, git checkout HEAD -- restores the file to its last committed state. This can be helpful to discard local changes.

SEE ALSO

git branch(1), git reset(1), git stash(1), git switch(1), git restore(1)

Copied to clipboard