【问题标题】:Git - Get only the new words from a commit compared to master commit/previous commitGit - 与主提交/先前提交相比,仅从提交中获取新词
【发布时间】:2020-02-02 16:24:54
【问题描述】:

如何仅获取添加到 git commit 的新文本(与以前的提交或 master 相比)。例如,假设我有来自先前提交的文本文件,其内容如下:

file 1:
-----------
hello my first name is john

并且文件被编辑并推送到:

file 1:
-----------
hello my last name is doe

我希望能够只获得差异词 - 例如在此示例中获取last doe,到标准输出或文本文件。

最简单的方法是什么?

【问题讨论】:

    标签: git diff git-diff


    【解决方案1】:

    让 git 用 git diff HASH --word-diff 比较单词而不是行:

    $ git diff HEAD^ --word-diff
    diff --git a/file.txt b/file.txt
    index 244f97f..ad2517e 100644
    --- a/file.txt
    +++ b/file.txt
    @@ -1 +1 @@
    hello my [-first-]{+last+} name is [-john-]{+doe+}
    

    将输出馈送到grep/sed/awk 以提取由{++} 包围的实际单词。我用grep -oP 做到了这一点,其中-P 启用perl 样式的正则表达式,-o 只显示找到的部分而不是整行:

    $ git diff HEAD^ --word-diff |grep -oP '(?<=(\{\+)).+?(?=(\+\}))'
    last
    doe
    

    正则表达式分解:

    • (?&lt;=(\{\+)) {+ 的正向后视,因此这些符号是匹配所必需的,但不包含在其中
    • .+? 懒惰搜索所有符号。没有懒惰会贪婪地包含第一个和最后一个括号之间的所有内容:last+} name is [-john-]{+doe
    • (?=(\+\})) +} 的正向前瞻,类似于后视

    注意:如果这些符号实际上是您更改的一部分,输出将不会正确包含 +} 和任何后续文本。

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2014-10-29
      • 2013-08-18
      • 2020-12-12
      • 2011-11-30
      • 2017-04-22
      • 2011-02-18
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多