10

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?

1 Answer 1

19

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 status says nothing about that file, it's in the index, it's just that :0:path/to/some/file has the same data as HEAD:path/to/some/file. If the file weren't in the index at all–in no staging slots—git status would tell you that the file is staged for deletion.

(Re the context: if you have space, I'd recommend doing a git checkout-index of every stage-zero-index-file into a temporary work area. To easily test whether all index files are at stage zero, use git write-tree, which fails if any files are in staging slots other than zero.)

Sign up to request clarification or add additional context in comments.

2 Comments

I knew it was something with the colon! Only that I remembered for the case when there is a conflict... not when there is no conflict. Thanks!
Oh, it's that simple. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.