Skip to content

checkout Git Command Guide

The git checkout command is used to switch branches or restore working tree files. It updates files in the working tree to match the version in the index or the specified tree, and can also update HEAD to set the specified branch as the current branch.

Terminal window
git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
git checkout [-f] <tree-ish> [--] <pathspec>...
git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
OptionDescription
-q, —quietSuppress feedback messages
-f, —forceForce checkout, ignore untracked files
-mAllow merging working tree changes
-b Create and checkout new branch
-B Reset existing branch and checkout
—detachDetach HEAD at specified commit
—ours, —theirsCheckout stage #2 (ours) or #3 (theirs) for merges
—conflict= Specify merge conflict resolution style
-p, —patchInteractively select hunks to checkout
—progressShow progress status
—orphan Create orphan branch
—trackSet up branch tracking (implied with -b)
—no-trackDon’t set up branch tracking
ParameterDescription
[]Branch to switch to (default: current if empty)
[]Starting point for new branch
[]Tree/commit to checkout files from
[…]Files/paths to checkout
Terminal window
git checkout develop

Switch to the develop branch and update working tree.

Terminal window
git checkout -b feature/new-feature

Create and switch to a new branch called feature/new-feature.

Terminal window
git checkout -f main

Force switch to main branch, discarding all local changes and untracked files.

Checkout specific files from another branch

Section titled “Checkout specific files from another branch”
Terminal window
git checkout origin/main -- README.md

Checkout README.md file from origin/main branch.

Terminal window
git checkout --orphan new-project

Create new orphan branch for completely different project history.

Terminal window
git checkout --detach HEAD~5

Detach HEAD at 5 commits back from current position.

Terminal window
git checkout -p HEAD~1 -- file.txt

Interactively choose which hunks to checkout from HEAD~1 for file.txt.

Resolve merge conflicts checking out our version

Section titled “Resolve merge conflicts checking out our version”
Terminal window
git checkout --ours conflicted-file.txt

Checkout our version (stage 2) of conflicted-file.txt.

Terminal window
git checkout -B feature/in-progress HEAD~10

Reset feature/in-progress branch to 10 commits back and switch to it.

To switch to an existing branch, run:

Terminal window
git checkout <branch-name>

How can I create a new branch and switch to it?

Section titled “How can I create a new branch and switch to it?”

To create a new branch and switch to it, use:

Terminal window
git checkout -b <new-branch-name>

How do I discard local changes and go back to last commit?

Section titled “How do I discard local changes and go back to last commit?”

To discard local changes and reset working tree, use:

Terminal window
git checkout -f HEAD

How can I checkout a specific file from another branch?

Section titled “How can I checkout a specific file from another branch?”

To checkout a specific file from another branch, run:

Terminal window
git checkout <branch> -- path/to/file

What’s the difference between git switch and git checkout?

Section titled “What’s the difference between git switch and git checkout?”

git switch focuses only on branch switching while git checkout has broader functionality including file and conflict resolution. git switch is more intuitive for branch operations.

How do I resolve merge conflicts by choosing sides?

Section titled “How do I resolve merge conflicts by choosing sides?”

To resolve conflicts by choosingvin our side, use:

Terminal window
git checkout --ours <file>
git checkout --theirs <file>

How can I inspect a historical commit without switching?

Section titled “How can I inspect a historical commit without switching?”

To inspect a historical commit without switching branches, use:

Terminal window
git checkout --detach <commit-hash>
  1. Switching between branches during development
  2. Creating feature branches for new work
  3. Restoring individual files from other commits
  4. Resolving merge conflicts by choosing sides
  5. Inspecting historical versions of files
  6. Temporarily checking out tags or commits for inspection