ErikE
ErikE

Reputation: 50191

git diff showing only lines that had a particular word added

I know of a commit in which a problematic change was introduced. However, the commit is so large that it's breaking gitlab, and I currently don't have a good tool for searching for a particular difference in the large commit.

Searching in git for a commit that introduces or removes a word is very simple, however, I already know the commit, and need to know instead which exact files/lines were affected. How can I do that?

Let's assume that the commit is sha abcdef1 and is the immediate ancestor of the current branch/HEAD. I'd like to search for the string word in the diff between this commit and the one before it.

Upvotes: 0

Views: 586

Answers (1)

Tgr
Tgr

Reputation: 28160

A simple approximation is git --color <log/diff/etc> -S <word> | ack --nocolor $'(^(?!\e\\[31m|\e\\[32m)|<word>)' which will use color marker matching to only show change lines containing the word (but will still show change headers). It's nice for filtering huge change blocks, not so nice when you have lots of changed files / blocks and most of them don't contain the word as the headers will still show up for those. You could use something more powerful than ack/grep such as awk to remember the last yellow block (commit id), white block (file header) and cyan block (change block header) and only show them when the word is found in a red/green line, but it would be a bit more complicated.

Upvotes: 1

Related Questions