Git - 与主 commit/previous 提交相比,仅从提交中获取新词
Git - Get only the new words from a commit compared to master commit/previous commit
如何只获取添加到 git 提交的新文本(与之前的提交或 master 相比)。例如,假设我有上一次提交的文本文件,内容如下:
file 1:
-----------
hello my first name is john
并且文件被编辑并推送到:
file 1:
-----------
hello my last name is doe
我只想得到差异词 - 例如在此示例中,获取 last doe
,到标准输出或文本文件。
最简单的方法是什么?
请 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
正则表达式细分:
(?<=(\{\+))
{+
的正后视,因此这些符号是匹配所必需的,但未包含在其中
.+?
懒惰搜索所有符号。没有懒惰会贪婪地包括第一个和最后一个括号之间的所有内容:last+} name is [-john-]{+doe
(?=(\+\}))
+}
的正面前瞻,类似于后视
注意:如果这些符号实际上是您所做更改的一部分,输出将不会正确包含 +}
和任何后续文本。
如何只获取添加到 git 提交的新文本(与之前的提交或 master 相比)。例如,假设我有上一次提交的文本文件,内容如下:
file 1:
-----------
hello my first name is john
并且文件被编辑并推送到:
file 1:
-----------
hello my last name is doe
我只想得到差异词 - 例如在此示例中,获取 last doe
,到标准输出或文本文件。
最简单的方法是什么?
请 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
正则表达式细分:
(?<=(\{\+))
{+
的正后视,因此这些符号是匹配所必需的,但未包含在其中.+?
懒惰搜索所有符号。没有懒惰会贪婪地包括第一个和最后一个括号之间的所有内容:last+} name is [-john-]{+doe
(?=(\+\}))
+}
的正面前瞻,类似于后视
注意:如果这些符号实际上是您所做更改的一部分,输出将不会正确包含 +}
和任何后续文本。