git 中的 CVS 风格差异
有没有办法让git diffs的输出格式像cvs风格的diffs一样?我发现 git diffs 的可读性较差。另外,more
中出现的 git diff 很烦人 - 我怎样才能禁用它?
Is there any way to make the output format of git diffs like cvs style diffs? I find git diffs to be less readable. Also, the git diffs appearing in more
are annoying - how can I disable this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
git diff
使用“通用”格式diff -u
。cvs diff
使用较旧的标准 diff 格式(已删除的行用<
标记,添加的行用>
标记,每个差异周围没有上下文) 。例如,您可以使用 git diff -c 或 git diff -C5 获取“上下文差异”,以获取 5 行上下文,而不是默认的 3 行。
< code>diff --normal (至少如果你使用的是 GNU diffutils)会产生一个旧式的 diff,但是
git diff
似乎无法识别- -正常
选项。就我个人而言,我发现上下文差异比旧式差异更具可读性,因此我从未发现需要将旧式差异与 git diff 一起使用。尝试 git diff -c (以及 haggai_e 禁用寻呼机的建议)。
如果您确实想要旧式差异(
<
、>
,无上下文),可能有一种方法可以做到。编辑:
如果
git diff
不符合您的要求,您可以提取文件修订版本的副本,并使用您喜欢的任何差异工具。我自己的get-versions
工具可以提取多个版本来自 git(或来自 RCS 或 CVS)的文件。这是另一个解决方案,尽管它有点复杂。例如,我为
git
本身克隆了 git 存储库。如果我想比较顶级README
文件的两个连续版本,我可以这样做:将其包装在一个小脚本中很容易。
请注意,
:
后面的文件路径(上例中的README
)是相对于存储库的根目录,而不是相对于当前目录。您可以在名称前添加./
使其相对于当前目录。 (后者可能不适用于某些旧版本的 git。)git diff
uses the "universal" format,diff -u
, by default.cvs diff
uses the older standard diff format (deleted lines marked with<
, added lines marked with>
, no context around each difference).You can get a "context diff" using
git diff -c
, orgit diff -C5
, for example, to get 5 lines of context rather than the default 3.diff --normal
(at least if you're using GNU diffutils) will produce an old-style diff, butgit diff
doesn't seem to recognize the--normal
option.Personally, I find context diffs much more readable than old-style diffs, so I've never found the need to use old-style diffs with
git diff
. Trygit diff -c
(along with haggai_e's suggestion to disable the pager).If you really want the old-style diffs (
<
,>
, no context), there's probably a way to do it.EDIT :
If
git diff
doesn't do what you want, you can extract copies of the revisions of the file and use whatever diff tool you like on them. My ownget-versions
tool can extract multiple versions of a file from git (or from RCS or CVS).And here's another solution, though it's a bit more complicated. As an example, I've cloned the git repo for
git
itself. If I want to compare two successive versions of the top-levelREADME
file, I can do this:It would be easy enough to wrap this in a small script.
Note that the file path following the
:
(README
in the above example) is relative to the root of the repository, not to the current directory. You can precede the name with./
to make to make it relative to the current directory. (The latter might not work with some older versions of git.)您可以通过将 core.pager 设置为空字符串来禁用寻呼机:
You can disable the pager by setting
core.pager
to an empty string:尝试使用标准 diff 格式:
这会提供类似 CVS 的换行符和旧行,没有上下文,但它不会在不同行组之间插入额外内容。不太确定如何实现其余部分,因为我不知道您想要 CVS 式格式的程度。
有关更多信息,
另请参阅
但我怀疑这是否符合您的要求。如果这对您有用,要使其每次与 git diff 一起工作,请参阅 使用以下命令配置 diff 工具.gitconfig?,引用 http://jeetworks.com/node/90在接受的答案中。
Try playing with standard diff formatting:
This gives CVS-like newlines and oldlines, with no context, but it doesn't insert the extras between groups of different lines. Not quite sure how to achieve the rest, given I don't know the extent to which you would like CVS-like formatting.
For more information
Also see
But I doubt this does what you want. If this does work for you, to make it work every time with git diff, see Configuring diff tool with .gitconfig?, which references http://jeetworks.com/node/90 in the accepted answer.