3

git diff --staged allows you to view changes between HEAD and the staged changes.

How do I view the full file as it would exist in HEAD if I were to commit the staged changes? How do I view a specific subset of lines from it?

augustin
  • 14,373
  • 13
  • 66
  • 79

2 Answers2

5

You can view the staged version of a file using:

git cat-file -p :./FILENAME

The leading : causes git to read from the index. The ./ gets it to look in the current directory, this part may be omitted if you specify a path relative to the top of the repository rather than to your current directory.

You can view a specific subset of lines by piping the output from that to another command to do the desired limiting as for any other file.

qqx
  • 18,947
  • 4
  • 64
  • 68
  • +1 Thanks. I do "git cat-file -p :./FILENAME.php > temporary.php". This way I can view the file with vi, with syntax highlighting and all. Cool. Thanks :) – augustin Aug 03 '13 at 11:20
  • 1
    You could also pipe to `vim +':set ft=php' -` to do that without a temporary file. – qqx Aug 03 '13 at 11:30
1
$ git ls-files --cached --stage
100644 f009acdec84ee338ec99c92920d603a67241608f 0       README

Guest@HOME-PC ~/doubt (master)
$ git cat-file -p f009acdec84ee338ec99c92920d603a67241608f

One
Teo
forvaidya
  • 3,041
  • 3
  • 26
  • 33
  • This **would** show the file with staged changes. The `--cached` option isn't actually needed, it's the default. – qqx Aug 03 '13 at 11:02