简体   繁体   English

在第一次和最后一次提交之间的同一文件中使用git diff

[英]Using git diff in same file between first and last commit

I have a text file (.yml) that I enter some texts to my system use. 我有一个文本文件(.yml),我输入一些文本到我的系统使用。 But now we need to know what new lines at once. 但现在我们需要立刻知道新的线路。 I thought to use git to solve our problem, but I cant find a clarified command to do this. 我想用git来解决我们的问题,但我找不到明确的命令来做到这一点。

Is this possible? 这可能吗?

You can get a list of the commits withs something like so: 你可以得到一个类似的提交列表:

git log --abbrev-commit --pretty=oneline

Which should something like this: 这应该是这样的:

commitD Description
commitC Description
commitB Description
commitA Description

Choose your two commits and substitute them below: 选择你的两个提交并在下面替换它们:

git diff <commitA>..<commitB> -- /path/to/file

You can do it with one commandline: 您可以使用一个命令行执行此操作:

git diff -w `git rev-list HEAD --reverse | head -1`..HEAD -- <path/file>

It does not work to use --reverse with a count of 1 (-1 / --max-count 1) ... as it reverses the resulting list after truncating it. 它不能使用--reverse,计数为1(-1 / --max-count 1)...因为它在截断后反转结果列表。 So either you use --reverse together with the head command or you use tail -1. 因此,要么使用--reverse与head命令一起使用,要么使用tail -1。

I had to add the -w for my usage because of switches in line-endings and tab vs. spaces usage. 我必须为我的用法添加-w,因为行结尾和制表符与空格使用的开关。

This may not work as expected if the file was not added in the first commit! 如果在第一次提交中未添加文件,则可能无法按预期工作! You may change the above line to: 您可以将以上行更改为:

git diff -w `git rev-list HEAD --reverse -- <path/file> | head -1`..HEAD -- <path/file>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM