Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git checkout-index command

Reference for git checkout-index: copy files from the index to the working tree or to another folder with --prefix, restore deleted files, and avoid overwriting.

git checkout-index writes files recorded in the index out to disk. With no extra options it refuses to overwrite files that already exist; with -f it replaces them. Combined with --prefix, it can export the whole staged snapshot into a different directory, without needing a working tree at all.

It is the low-level sibling of git restore and git checkout -- <path>. Those commands are friendlier and also deal with attributes and sparse checkouts in a consistent way; checkout-index remains useful for scripts that export or deploy files.

Terminal
git checkout-index -a -f
git checkout-index --prefix=out/ -a
git checkout-index -- path/to/file

Try it

Bash Restore a deleted file from the index

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Export a snapshot to another folder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git checkout-index [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
                   [--stage=<number>|all] [--temp] [--ignore-skip-worktree-bits]
                   [-z] [--stdin] [--] [<file>...]
Name Description
<file>... Paths to write. Without -a or --stdin you must list at least one.
-a, --all Check out every file in the index.
-f, --force Overwrite existing files.
-n, --no-create Update only files that already exist.
-q, --quiet Do not complain about files that exist or are missing.
-u, --index Refresh the index's stat information for the files written.
--prefix=<string> Prepend this string to every path; it must end in / to mean a directory. The directory is created if needed.
--stage=<1|2|3|all> Check out a specific stage of an unmerged file (see conflicts below).
--temp Write each file to a temporary name and print the mapping instead of using the real path.
--stdin, -z Read paths from standard input, optionally NUL-separated.

Returns: nothing, apart from the --temp mapping.

Errors: "already exists, no checkout" when a file is present and -f is missing (the other files are still written); "is not in the cache" for unknown paths.

Usage notes

  • Reads the index, not a commit. To export a commit, use git archive or git read-tree into a temporary index with GIT_INDEX_FILE, then checkout-index -a --prefix=....
  • Unstaged edits are lost with -f. Because the file content comes from the index, forcing overwrites your modifications in the working tree.
  • Stat refresh: run git update-index --refresh first, or pass -u, in scripts that later call git status, so Git does not re-hash every file.
  • Conflicts: an unmerged path has stages 1 (base), 2 (ours) and 3 (theirs). --stage=all or --temp writes all three to files so you can run git merge-file on them.
  • Attributes are applied: text, eol and filter settings (including LFS) take effect, exactly as on checkout.
  • Prefer git restore for ordinary recovery: git restore a.txt does the same job with simpler options.