【问题标题】:Using an alternate diff algorithm in Git在 Git 中使用另一种差异算法
【发布时间】:2011-08-25 08:43:52
【问题描述】:

由于git是为源代码设计的,其默认的diff算法将一行视为最小的不可分割单元。

我正在尝试编辑一些在第 80 列换行的 markdown 文件。添加一个句子可能会导致段落的其余部分被标记为已更改。

有没有办法让 Git 使用更适合文本的 diff 算法?我需要一个将单词或句子视为不可分割的单元而不是行吗?

【问题讨论】:

    标签: git diff


    【解决方案1】:

    你可以试试git diff --word-diff

    $ git diff --word-diff
    diff --git a/test.txt b/test.txt
    index 54585bb..a8cd97e 100644
    --- a/test.txt
    +++ b/test.txt
    @@ -1,7 +1,7 @@
    Because git is designed for source code, its diff algorithms {+are bibbity +}
    {+bobbity boo+} treat a line as the minimum indivisible unit. I am trying to edit 
    some markdown files that are word wrapped at column 80. Adding a sentence can 
    cause the rest of the paragraph to be marked as changed.
    
    Is there a way to have Git use a diff algorithm more suited to text? One that 
    treats words or sentences as indivisible units rather then lines?
     No newline at end of file
    

    【讨论】:

    【解决方案2】:

    也许你正在寻找word-diff

    --word-diff[=<mode>]

    显示单词差异,使用<mode> 分隔改变的单词。默认, 单词由空格分隔;看 --word-diff-regex 下面。 <mode> 默认为普通,并且必须是以下之一:

    颜色

    仅使用突出显示更改的单词 颜色。暗示--颜色。

    普通

    将单词显示为 [-removed-] 和 {添加}。不尝试逃跑 分隔符(如果它们出现在 输入,所以输出可能不明确。

    瓷器

    使用特殊的基于行的格式 用于脚本消费。 添加/删除/未更改的运行是 打印在通常的统一差异中 格式,以 +/-/ 开头 行首的字符 并延伸到行尾。 表示输入中的换行符 由一个波浪号〜在它自己的一行上。

    没有

    再次禁用单词差异。

    请注意,尽管名称 第一种模式,颜色用于高亮 如果在所有模式下更改的部分 已启用。

    http://git-scm.com/docs/git-diff

    【讨论】:

      【解决方案3】:

      这是一个自定义的示例(来自this question)。默认情况下,--word-diff 假定单词是非空白字符的字符串。以下命令将认为一个单词由以下之一组成:

      1. 一串字母数字字符和下划线
      2. 单个非字符

      命令:

      git diff --color-words --word-diff-regex='[A-z0-9_]+|[^[:space:]]'
      

      【讨论】:

      猜你喜欢
      • 2016-03-15
      • 1970-01-01
      • 2018-08-21
      • 2012-09-14
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多