我应该如何使用 git diff 处理长行?

发布于 2024-07-05 19:24:47 字数 237 浏览 5 评论 0原文

我正在文件上运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

小嗷兮 2024-07-12 19:24:47

到目前为止还没有人指出这一点。 它非常容易记住,不需要在 git config 中进行额外的配置

git diff --color | less -R

Noone pointed out this till now. Its quite simple to remember and no extra configuration needs to be done in the git config

git diff --color | less -R
深海少女心 2024-07-12 19:24:47

或者,如果您使用 less 作为默认分页器,只需在查看差异时键入 -S 即可重新启用 less 换行。

Or if you use less as default pager just type -S while viewing the diff to reenable wrapping in less.

夏有森光若流苏 2024-07-12 19:24:47

git diff 输出的显示由您使用的任何分页器处理。

通常,在 Linux 下,会使用 less

您可以通过设置 GIT_PAGER 环境变量来告诉 git 使用不同的分页器。 如果您不介意分页(例如,您的终端允许您向后滚动),您可以尝试将 GIT_PAGER 显式设置为空以停止使用分页器。 在 Linux 下:

$ GIT_PAGER='' git diff

如果没有寻呼机,则换行。

如果您的终端不支持彩色输出,您还可以使用 --no-color 参数将其关闭,或者在 git 配置文件的颜色部分中添加一个条目。

$ GIT_PAGER='' git diff --no-color

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 setting GIT_PAGER to empty to stop it using a pager. Under 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.

$ GIT_PAGER='' git diff --no-color
鼻尖触碰 2024-07-12 19:24:47

您还可以使用 git config 来设置分页器进行换行。

$ git config core.pager 'less -r' 

设置当前项目的寻呼机设置。

$ git config --global core.pager 'less -r' 

为所有项目全局设置寻呼机

You can also use git config to setup pager to wrap.

$ 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

小帐篷 2024-07-12 19:24:47

完全归功于 Josh Diehl 这个答案的评论,我尽管如此,我觉得这应该是一个答案,因此添加它:

处理长行中差异的一种方法是使用面向单词的差异。 这可以通过以下方式完成:

git diff --word-diff

在这种情况下,您将获得显着不同的 diff 输出,具体显示行内发生的更改。

例如,

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

您可能会得到这样的结果:

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

或者,通过着色,而不是这样的:

result of只是 git diff

你可能会得到这个:

result of git diff --word-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:

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.

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:

result of just git diff

You might get this:

result of git diff --word-diff

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.

新一帅帅 2024-07-12 19:24:47

从 Git 1.5.3 开始
(2007 年 9 月)

--no-pager 选项有已可用。

git --no-pager diff

如何防止 git diff 使用寻呼机?

示例

从 v2.1 开始,换行是默认

Git v2.1 发行说明< /a>

Since Git 1.5.3
(Sep 2007)

a --no-pager option has been available.

git --no-pager diff

How do I prevent git diff from using a pager?

Example

Starting with v2.1, wrap is the default

Git v2.1 Release Notes

妄想挽回 2024-07-12 19:24:47

要使用 less 作为寻呼机并使换行永久化,您只需启用折叠长行选项:

git config --global core.pager 'less -+S'

这样您就不必在使用 less 时键入它。

干杯

To use less as the pager and make line wrapping permanent you can simply enable the fold-long-lines option:

git config --global core.pager 'less -+S'

This way you do not have to type it while using less.

Cheers

最舍不得你 2024-07-12 19:24:47

刚刚用谷歌搜索到了这个。 GIT_PAGER='less -r' 对我有用

Just googled up this one. GIT_PAGER='less -r' works for me

痴意少年 2024-07-12 19:24:47

Mac OSX:除了someone45的“-S”之外,其他答案都没有,而 less is running 对我有用。 需要执行以下操作才能使自动换行持久化:

git config --global core.pager 'less -+$LESS -FRX'

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:

git config --global core.pager 'less -+$LESS -FRX'
温馨耳语 2024-07-12 19:24:47

八年后,我找到了一个更好的答案,来自 https://superuser.com/questions/777617/line-wrapping-less-in-os-x-specially-for-use-with-git-diff

git config core.pager `fold -w 80 | less`

现在您可以通过折叠管道传输 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:

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.

淡水深流 2024-07-12 19:24:47

当您使用“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.

溺孤伤于心 2024-07-12 19:24:47

您可以简单地将 git diff 的输出通过管道传输到 more:

git diff | more

You could simply pipe the output of git diff to more:

git diff | more
骑趴 2024-07-12 19:24:47

这不是一个完美的解决方案,但 gitk 和 git-gui 都可以显示此信息,
并有滚动条。

Not a perfect solution, but gitk and git-gui can both show this information,
and have scrollbars.

肩上的翅膀 2024-07-12 19:24:47

列出当前/默认配置:

  $ git config --global core.pager  
    less -FXRS -x2

然后更新并省略 -S,例如:

  $ git config --global core.pager 'less -FXR -x2'

-S:导致比屏幕宽度长的行被截断而不是折叠。

list the current/default config:

  $ git config --global core.pager  
    less -FXRS -x2

then update and leave out the -S like:

  $ git config --global core.pager 'less -FXR -x2'

the -S: Causes lines longer than the screen width to be chopped rather than folded.

摇划花蜜的午后 2024-07-12 19:24:47

当遇到麻烦时,我经常求助于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文