Nic Cottrell
Nic Cottrell

Reputation: 9665

How to grep the git diff to ignore changes without a certain keyword?

I need to do an inverse git diff after a refactor (where I changed the logging library), to find a few lines unrelated to the main refactor that I need to commit sepearately. I've read this question and the -G flag looks promising but how to inverse that? i.e. changes that don't include the string log.

Upvotes: 2

Views: 718

Answers (1)

Schwern
Schwern

Reputation: 165004

Use your pager for this sort of task. Your pager is the program that allows you to go through the output of things like git diff and git log and many, many other commands a page at a time. It has powerful features for searching, and those features work regardless of the underlying command.

The usual pager these days is less, a play on the previous default more. man less will allow you read about all its features, including...

   /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.

So / then ! and you'll see Non-match /. Then type log or whatever you want to skip. less will still highlight all the instances of log, but you can move forward and back through all the non-matching lines with n and N.


A better solution is to git add just those lines separately using git add -p. This will allow you to stage individual hunks of the change one at a time, skipping changes or entire files. Then commit just those pieces. See Interactive Staging in the Git Book.

Upvotes: 3

Related Questions