Git command to retrieve contents of staged file?

135 Views Asked by At

Is it possible to retrieve the contents of a staged file in the current index using Git? The problem is that if I access the source on the disk I would get the unstaged changes returned with the blob.

I am trying to create a git pre-commit hook to update a file with checksums when changes are being committed to a Git repository. So far I managed to list all tracked files with git ls-files and all files that are staged in the commit with git diff --cached --name-only 2>&1.

I am looking for something like git show-raw-contents --cached --file="path/to/tracked.file" (Yes I just made that up)

Google, Stack Overflow and the Git documentation is not my friend this week. A command supported by pre-commit hooks would be great. But post-commit works just as well.

1

There are 1 best solutions below

0
tim On

The answer to the question is

git cat-file blob :path/to/tracked.file 2>&1

Works in both pre-commit and post-commit hooks.