51

After reading Git pre-commit hook : changed/added files, the following question arose:

Given I have a file with both staged and unstaged changes, how can I display a preview of the file's contents after staging?

Example:

echo "foo" >> file
git add file
echo "bar" >> file

Wanted output:

[previous contents of file]
foo
5

4 Answers 4

127

Use the : prefix to access objects in the current index (staged but not yet commited).

git show :file

See gitrevisions (which uses the term 'stage 0' for the "normal" index contents):

:[<n>:]<path>, e.g. :0:README, :README

A colon, optionally followed by a stage number (0 to 3) and a colon, followed by a path, names a blob object in the index at the given path. A missing stage number (and the colon that follows it) names a stage 0 entry. During a merge, stage 1 is the common ancestor, stage 2 is the target branch’s version (typically the current branch), and stage 3 is the version from the branch which is being merged.

7
  • Very nice indeed, however I'd prefer something that works with relative paths. Even though the docs state that A path starting with ./ or ../ is relative to current working directory., this doesn't work for me. Mar 1, 2011 at 12:15
  • So given a dirty file README; HEAD:README shows file before changes. :README shows file with staged changes only. Is there any way to show the file with only non staged changes? I didn't see anything that would indicate so in the linked documentation.
    – JD Isaacks
    Sep 12, 2013 at 3:26
  • @JDIsaacks: Not directly, no, because such a file does not exist anywhere in the repository (while :README exists in the index and HEAD:README exists in the current commit). You could, however, try applying git diff README output (which only shows unstaged changes) to a temporary copy of HEAD:README...
    – user1686
    Sep 12, 2013 at 13:41
  • 1
    Note that the syntax is now (has long been) formalized as :<digit>:<path>, where the digit indicates which index slot to use: 0 (zero) is the slot containing the file that will be committed, while slots 1, 2, and 3 are used only during a merge conflict.
    – torek
    May 18, 2018 at 14:20
  • 1
    True - I'm suggesting that the answer could note that there's a fuller syntax to get at any of the possible index slots. (But the link to gitrevisions is probably sufficient)
    – torek
    May 18, 2018 at 20:15
6

Update: the answer from grawity has a much neater solution

This recipe is from jleedev's answer to another question:

git cat-file blob $(git ls-files -s file | awk '{print $2}')

You might want to create a git alias for that if you're going to use it often.

2
  • Exactly what I wanted. Unfortunately, my search didn't bring up that other question. Mar 1, 2011 at 10:38
  • @Pumbaa80: thanks, but I think grawity's answer is better, and the one that should be accepted :) Mar 1, 2011 at 10:48
0

You can do git diff --cached, but this isn't exactly what you want.

git grep -h --cached ^ -- file

works for me.

-2

Just have a look at the top answer for this question: How do I show the changes which have been staged?

The --cached option is what you want.

2
  • This will show the the diff of the staged file, but it won't show you the way the file would look if you just opened it in an editor or cat it.
    – user456814
    Aug 10, 2013 at 18:33
  • Apologies for the downvote, but that's a perfectly good answer to a completely different question! :smile: Oct 14, 2014 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.