我应该如何使用 git diff 处理长行?
我正在文件上运行 git-diff ,但更改位于长线的末端。
如果我使用光标键向右移动,它就会丢失颜色编码 - 更糟糕的是,线条不会对齐 - 使得跟踪更改变得更加困难。
有没有办法防止这个问题或者简单地换行?
我正在通过 mingw32 运行 Git 1.5.5。
I'm running git-diff on a file, but the change is at the end of a long line.
If I use cursor keys to move right, it loses colour-coding—and worse the lines don't line up—making it harder to track the change.
Is there a way to prevent that problem or to simply make the lines wrap instead?
I'm running Git 1.5.5 via mingw32.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
到目前为止还没有人指出这一点。 它非常容易记住,不需要在 git config 中进行额外的配置
Noone pointed out this till now. Its quite simple to remember and no extra configuration needs to be done in the git config
或者,如果您使用 less 作为默认分页器,只需在查看差异时键入
-S
即可重新启用 less 换行。Or if you use less as default pager just type
-S
while viewing the diff to reenable wrapping in less.git diff 输出的显示由您使用的任何分页器处理。
通常,在 Linux 下,会使用
less
。您可以通过设置 GIT_PAGER 环境变量来告诉 git 使用不同的分页器。 如果您不介意分页(例如,您的终端允许您向后滚动),您可以尝试将
GIT_PAGER
显式设置为空以停止使用分页器。 在 Linux 下:如果没有寻呼机,则换行。
如果您的终端不支持彩色输出,您还可以使用
--no-color
参数将其关闭,或者在 git 配置文件的颜色部分中添加一个条目。The display of the output of
git diff
is handled by whatever pager you are using.Commonly, under Linux,
less
would be used.You can tell git to use a different pager by setting the
GIT_PAGER
environment variable. If you don't mind about paging (for example, your terminal allows you to scroll back) you might try explicitly settingGIT_PAGER
to empty to stop it using a pager. Under Linux:Without a pager, the lines will wrap.
If your terminal doesn't support coloured output, you can also turn this off using either the
--no-color
argument, or putting an entry in the color section of your git config file.您还可以使用 git config 来设置分页器进行换行。
设置当前项目的寻呼机设置。
为所有项目全局设置寻呼机
You can also use
git config
to setup pager to wrap.Sets the pager setting for the current project.
Sets the pager globally for all projects
完全归功于 Josh Diehl 对这个答案的评论,我尽管如此,我觉得这应该是一个答案,因此添加它:
处理长行中差异的一种方法是使用面向单词的差异。 这可以通过以下方式完成:
在这种情况下,您将获得显着不同的 diff 输出,具体显示行内发生的更改。
例如,
您可能会得到这样的结果:
或者,通过着色,而不是这样的:
你可能会得到这个:
现在,如果你正在比较一条非常长的线,您可能仍然对您最初描述的寻呼机情况有问题,并且在其他答案中已得到解决,显然令人满意。 不过,希望这为您提供了一个新工具,可以更轻松地识别线路上发生了什么变化。
With full credit to Josh Diehl in a comment to this answer, I nevertheless feel like this ought to be an answer unto itself, so adding it:
One way to deal with seeing differences in long lines is to use a word-oriented diff. This can be done with:
In this case, you'll get a significantly different diff output, that shows you specifically what has changed within a line.
For example, instead of getting something like this:
You might get something like this:
Or, with colorization, instead of this:
You might get this:
Now, if you're comparing a really long line, you may still have issues with the pager situation you originally described, and which has been addressed, apparently to satisfaction, in other answers. Hopefully this gives you a new tool, though, to more easily identify what on the line has changed.
从 Git 1.5.3 开始
(2007 年 9 月)
--no-pager
选项有已可用。如何防止 git diff 使用寻呼机?
示例
从 v2.1 开始,换行是默认
Git v2.1 发行说明< /a>
Since Git 1.5.3
(Sep 2007)
a
--no-pager
option has been available.How do I prevent git diff from using a pager?
Example
Starting with v2.1, wrap is the default
Git v2.1 Release Notes
要使用 less 作为寻呼机并使换行永久化,您只需启用折叠长行选项:
这样您就不必在使用 less 时键入它。
干杯
To use less as the pager and make line wrapping permanent you can simply enable the fold-long-lines option:
This way you do not have to type it while using less.
Cheers
刚刚用谷歌搜索到了这个。
GIT_PAGER='less -r'
对我有用Just googled up this one.
GIT_PAGER='less -r'
works for meMac OSX:除了someone45的“-S”之外,其他答案都没有,而 less is running 对我有用。 需要执行以下操作才能使自动换行持久化:
Mac OSX: None of the other answers except someone45's '-S' while less is running worked for me. It took the following to make word-wrap persistent:
八年后,我找到了一个更好的答案,来自 https://superuser.com/questions/777617/line-wrapping-less-in-os-x-specially-for-use-with-git-diff:
现在您可以通过折叠管道传输 git diff ,首先,然后到 less:wrapped,less page-height 是正确的,保持语法高亮。
Eight years later I find a superior answer, from https://superuser.com/questions/777617/line-wrapping-less-in-os-x-specifically-for-use-with-git-diff:
Now you pipe the git diff through fold, first, then to less: wrapped, less page-height is correct, keep syntax highlighting.
当您使用“git diff”并且它显示多个页面(您在页面末尾看到“:”)时,您可以键入“-S”并按 Enter 键。(S 应该是大写)。 它将切换折叠长线。
When you are using "git diff" and it's showing several pages(you see ":" at the end of the page)in this case you can type "-S" and press enter.(S should be capital). it will toggle fold long lines.
您可以简单地将 git diff 的输出通过管道传输到 more:
You could simply pipe the output of git diff to more:
这不是一个完美的解决方案,但 gitk 和 git-gui 都可以显示此信息,
并有滚动条。
Not a perfect solution, but
gitk
andgit-gui
can both show this information,and have scrollbars.
列出当前/默认配置:
然后更新并省略 -S,例如:
-S:导致比屏幕宽度长的行被截断而不是折叠。
list the current/default config:
then update and leave out the -S like:
the -S: Causes lines longer than the screen width to be chopped rather than folded.
当遇到麻烦时,我经常求助于 DiffMerge。 出色的差异工具,具有内嵌差异突出显示功能。 此外,在最新版本中,他们添加了一种具有水平模式的模式。
不过,我还无法配置 git 来使用它。 所以我确实必须首先获得该文件的两个版本。
When in trouble, I often resort to DiffMerge. Excellent diff tool that has in-line diff highlighting. Also, in the latest versions they added a mode to have an horizontal mode.
I haven't been able to configure git to use it, though. So I do have to muck around to get both versions of the file first.