【问题标题】:How to grep the git diff to ignore changes without a certain keyword?如何 grep git diff 忽略没有特定关键字的更改?
【发布时间】:2017-08-28 03:02:12
【问题描述】:

我需要在重构(我更改了日志库)之后执行逆 git diff,以找到与我需要单独提交的主要重构无关的几行。我读过这个question 并且-G 标志看起来很有希望,但如何反转呢?即不包含字符串 log 的更改。

【问题讨论】:

  • -G 接受一个正则表达式,所以为“不包含'log'”写一个正则表达式。
  • 我认为没有一种简单的方法可以实现这一点,最接近的方法可能是: git diff --unified=0 (摆脱大部分混乱)然后管道结果变成这样的反向匹配: grep -v "log"

标签: git


【解决方案1】:

使用您的pager 来完成此类任务。您的寻呼机是一个程序,它允许您一次处理一个页面的输出,例如 git diffgit log 以及许多其他命令。它具有强大的搜索功能,并且无论底层命令如何,这些功能都可以正常工作。

现在常用的寻呼机是less,这是对以前默认的more 的一种发挥。 man less 将让您了解它的所有功能,包括...

   /pattern
          Search  forward  in  the  file  for the N-th line containing the pattern.  N
          defaults to 1.  The pattern is a regular expression, as  recognized  by  the
          regular  expression  library  supplied by your system.  The search starts at
          the first line displayed (but see the -a and -j options, which change this).

          Certain  characters  are special if entered at the beginning of the pattern;
          they modify the type of search rather than become part of the pattern:

          ^N or !
                 Search for lines which do NOT match the pattern.

所以/ 然后! 你会看到Non-match /。然后输入log 或任何您想跳过的内容。 less 仍将突出显示 log 的所有实例,但您可以在所有与 nN 不匹配的行中前后移动。


更好的解决方案是使用git add -p 单独使用git add 这些行。这将允许您一次进行一个单独的更改,跳过更改或整个文件。然后只提交这些部分。见Interactive Staging in the Git Book

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2014-04-18
    • 2012-09-09
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多