3

I can see list of files using below command

git ls-files --cache

Wondering if it's possible to read the content of the listed file?

phd
  • 82,685
  • 13
  • 120
  • 165
Isaac
  • 12,042
  • 16
  • 52
  • 116

2 Answers2

4

Use git cat-file or git show. For example

for f in `git ls-files --cache`; do
    echo -- "----- File (cat): $f -----"
    git cat-file -p HEAD:$f
    echo -- "----- File (show): $f -----"
    git show HEAD:$f
    echo -- "----- End of File: $f -----"
done
phd
  • 82,685
  • 13
  • 120
  • 165
  • Correct me if I'm wrong. If I run a command like `git show HEAD:yarn.lock`, then from where will it pick the content from? From `HEAD`? Or from `git cache`? – Isaac Jul 13 '19 at 01:17
  • 2
    From the `HEAD`. Do you want index? (It's now called index, not cache). Just use `:$file`. – phd Jul 13 '19 at 01:42
1

You could use checkout-index. This command copies a file from the git cache (index) into the worktree.

Normally it overrides the working copy, but with the parameter --temp you could create a copy of your file.

git checkout-index --temp -- myfile.ext
Rene
  • 5,730
  • 17
  • 20