I can do git show <some-commit>:path/to/some/file but what do I replace <some-commit> to get the currently staged version of the file?
Context: I'm writing a pre-commit hook and I want to run a check on the staged files without resorting to stashing unstaged changes. Is there a better way?
Use
git show :path/to/some/file.Files that are "staged" represent an index copy of the file that differs from some other copy, such as the current commit copy that you can access via
HEAD:path/to/some/file.Files that are stored in the index have a stage number, usually zero. (Staging slots 1, 2, and 3 are used during conflicted merges.)
To refer to the copy of the file named F (in your case F =
path/to/some/file), use the revision specifier:number:F. In this case that's:0:path/to/some/file. When the number is zero—which it usually is—you can omit one colon and the zero, leaving:path/to/some/file.Note that when
git statussays nothing about that file, it's in the index, it's just that:0:path/to/some/filehas the same data asHEAD:path/to/some/file. If the file weren't in the index at all–in no staging slots—git statuswould tell you that the file is staged for deletion.(Re the context: if you have space, I'd recommend doing a
git checkout-indexof every stage-zero-index-file into a temporary work area. To easily test whether all index files are at stage zero, usegit write-tree, which fails if any files are in staging slots other than zero.)