Git: Show content of file as it will look like after committing

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

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

git show :file

See gitrevisions (emphasis mine):

<rev>:<path>, e.g. HEAD:README, :README, master:./README

A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon. :path (with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path .


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.


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

git grep -h --cached ^ -- file

works for me.

链接地址: http://www.djcxy.com/p/50870.html

上一篇: 如何让git状态仅显示暂存的文件

下一篇: Git:显示提交后的文件内容