bash Git:按提交后的样子显示文件内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5153199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Show content of file as it will look like after committing
提问by user123444555621
After reading Git pre-commit hook : changed/added files, the following question arose:
阅读Git pre-commit hook : changed/ added files 后,出现了以下问题:
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
回答by user1686
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):
请参阅gitrevisions(使用术语“阶段 0”表示“正常”索引内容):
:[<n>:]<path>, e.g. :0:README, :READMEA 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.
:[<n>:]<path>, e.g. :0:README, :README一个冒号,可选地后跟一个阶段编号(0 到 3)和一个冒号,后跟一个路径,在给定路径的索引中命名一个 blob 对象。缺少的阶段编号(以及它后面的冒号)命名为阶段 0 条目。在合并期间,阶段 1 是共同祖先,阶段 2 是目标分支的版本(通常是当前分支),阶段 3 是正在合并的分支的版本。
回答by Mark Longair
Update: the answer from grawityhas a much neater solution
This recipe is from jleedev's answer to another question:
这个食谱来自jleedev 对另一个问题的回答:
git cat-file blob $(git ls-files -s file | awk '{print }')
You might want to create a git aliasfor that if you're going to use it often.
如果您要经常使用它,您可能想为此创建一个git 别名。
回答by Tollef Fog Heen
You can do git diff --cached, but this isn't exactly what you want.
您可以执行 git diff --cached,但这并不是您想要的。
git grep -h --cached ^ -- file
git grep -h --cached ^ -- file
works for me.
对我来说有效。
回答by Daniel B?hmer
Just have a look at the top answer for this question: How do I show the changes which have been staged?
只需查看此问题的最佳答案:如何显示已上演的更改?
The --cachedoption is what you want.
该--cached选项是您想要的。

