-1

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.

tim
  • 2,530
  • 3
  • 26
  • 45
  • 3
    Does this answer your question? [How to retrieve a single file from a specific revision in Git?](https://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-a-specific-revision-in-git) – terrorrussia-keeps-killing Jan 04 '22 at 20:49
  • 2
    In your case it would look like `git show @:path/to/tracked.file` where `@` is the current head. – terrorrussia-keeps-killing Jan 04 '22 at 20:50
  • @fluffy I'm not certain if that works with the staged files for commit during pre-commit. I will try `git show @:path/to/tracked.file` but that's only gonna work post commit yeah? – tim Jan 04 '22 at 20:54
  • 1
    Index seems to be available without an explicit prefix https://stackoverflow.com/questions/5032188/how-can-i-get-content-of-a-file-from-git-index/5153415 : `git show :/path/to/tracked.file`. – terrorrussia-keeps-killing Jan 04 '22 at 20:57
  • @fluffy This works for post-commits `git cat-file blob :path/to/tracked.file`. Do you know if it works for staged files in pre-commits? – tim Jan 04 '22 at 21:02
  • Yup, it works, have just checked it myself for `.git/hooks/pre-commit`. – terrorrussia-keeps-killing Jan 04 '22 at 21:13
  • I can confirm this answered my question. It retrieves the full source for the staged files: `git cat-file blob :path/to/tracked.file 2>&1` – tim Jan 04 '22 at 21:15
  • No worries about that. It has been closed because the question was marked as a duplicate for the first link I provided (usually mods do a lot of work so they may miss another useful comments and links). I didn't know that omitting the rev-selector will use index instead, so I posted another link that demonstrates omitting the rev-selector. Glad it could help you, by the way: it helped me too, because I was aware of the first syntax, but never knew that omitting the rev part will use index. So it's all fine! – terrorrussia-keeps-killing Jan 04 '22 at 21:20
  • 2
    @tim Someone has just reopened the question. The question btw asks how to "get the content of a committed file" so it was an _exact dupe_ of the other question. In the comments it was then clarified that it was rather about getting the content of files in the index, not of committed files. Feel free to update the question to reflect this and provide an answer – knittl Jan 04 '22 at 21:24
  • @knittl Yeii :) I have swapped the word commited with staged in the title and question to avoid confusion. Who wants to provide the answer `git cat-file blob :path/to/tracked.file 2>&1`? I can do it, but I can't accept my own answers. – tim Jan 04 '22 at 21:33
  • Just `git show :path/to/file` suffices; `git rev-parse :path/to/file` gives you the blob hash ID, as does `git ls-files --stage` (but the latter is obnoxiously verbose so you'd want to limit it, and the `git show` is all-in-one, hence much easier). It's a bit buried or obtuse, but this *is* in VonC's answer to the linked question. – torek Jan 05 '22 at 00:34
  • Does this answer your question? [How can I get content of a file from git index?](https://stackoverflow.com/questions/5032188/how-can-i-get-content-of-a-file-from-git-index) – kan Jan 05 '22 at 23:30

1 Answers1

0

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.

tim
  • 2,530
  • 3
  • 26
  • 45