我应该如何将 git diff 用于长行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/136178/
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
How should I use git diff for long lines?
提问by Peter Boughton
I'm running git-diffon a file, but the change is at the end of a long line.
我正在一个文件上运行git-diff,但更改位于长行的末尾。
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.
我正在通过 mingw32 运行 Git 1.5.5。
采纳答案by SpoonMeiser
The display of the output of git diff
is handled by whatever pager you are using.
输出的显示git diff
由您使用的任何寻呼机处理。
Commonly, under Linux, less
would be used.
通常,在 Linux 下,less
会使用。
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 setting GIT_PAGER
to empty to stop it using a pager. Under Linux:
您可以通过设置GIT_PAGER
环境变量来告诉 git 使用不同的寻呼机。如果您不介意分页(例如,您的终端允许您向后滚动),您可以尝试明确设置GIT_PAGER
为空以使用寻呼机停止它。在 Linux 下:
$ GIT_PAGER='' git diff
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.
如果您的终端不支持彩色输出,您也可以使用--no-color
参数将其关闭,或者在 git 配置文件的颜色部分添加一个条目。
$ GIT_PAGER='' git diff --no-color
回答by someone45
Or if you use less as default pager just type -S
while viewing the diff to reenable wrapping in less.
或者,如果您使用 less 作为默认寻呼机,只需-S
在查看差异时键入即可重新启用 less 包装。
回答by Shoan
You can also use git config
to setup pager to wrap.
您还可以使用git config
设置寻呼机进行换行。
$ git config core.pager 'less -r'
Sets the pager setting for the current project.
设置当前项目的寻呼机设置。
$ git config --global core.pager 'less -r'
Sets the pager globally for all projects
为所有项目全局设置寻呼机
回答by lindes
With full credit to Josh Diehlin a commentto 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:
处理在长行中看到差异的一种方法是使用面向单词的差异。这可以通过以下方式完成:
git diff --word-diff
In this case, you'll get a significantly different diff output, that shows you specifically what has changed within a line.
在这种情况下,您将获得显着不同的 diff 输出,它会具体显示一行中发生了什么变化。
For example, instead of getting something like this:
例如,而不是得到这样的东西:
diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
-this is a short line
+this is a slightly longer line
You might get something like this:
你可能会得到这样的东西:
diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
this is a [-short-]{+slightly longer+} line
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.
现在,如果您正在比较一条非常长的线路,您可能仍然遇到最初描述的寻呼机情况的问题,并且已经在其他答案中得到解决,显然很满意。不过,希望这为您提供了一个新工具,可以更轻松地识别线路上发生了什么变化。
回答by Daniel Montezano
To use less as the pager and make line wrapping permanent you can simply enable the fold-long-lines option:
要使用 less 作为寻呼机并使换行永久化,您只需启用 fold-long-lines 选项:
git config --global core.pager 'less -+S'
This way you do not have to type it while using less.
这样您就不必在使用较少的情况下键入它。
Cheers
干杯
回答by John Lemberger
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:
Mac OSX:除了someone45的'-S'而less is running之外,其他答案都没有对我有用。采取以下措施使自动换行持久化:
git config --global core.pager 'less -+$LESS -FRX'
回答by singingfish
Just googled up this one. GIT_PAGER='less -r'
works for me
刚刚用谷歌搜索了这个。 GIT_PAGER='less -r'
对我来说有效
回答by Steven Penny
Since Git 1.5.3 (Sep 2007)
自 Git 1.5.3(2007 年 9 月)起
a --no-pager
option has been available.
有一个--no-pager
选项可用。
git --no-pager diff
How do I prevent git diff from using a pager?
Starting with v2.1, wrap is the default
从 v2.1 开始,wrap 是默认的
回答by Thomson Comer
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:
八年后,我从https://superuser.com/questions/777617/line-wrapping-less-in-os-x-specifically-for-use-with-git-diff找到了一个更好的答案:
git config core.pager `fold -w 80 | less`
Now you pipe the git diff through fold, first, then to less: wrapped, less page-height is correct, keep syntax highlighting.
现在你通过 fold 管道 git diff,首先,然后到 less:wrapped,less page-height 是正确的,保持语法高亮。
回答by Amin
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”并且它显示多个页面时(您会在页面末尾看到“:”),在这种情况下,您可以键入“-S”并按回车键。(S 应该是大写)。它将切换折叠长线。