When I do a git diff, it shows lines that have been added:
+ this line is added
lines that have been removed:
- this line i
Here's another, simpler way to find only lines which have been modified, and hence begin with a single +
or -
, while retaining color output:
git diff -U0 --color=always HEAD~ | grep --color=never -E $'^\e\[(32m\+|31m-)'
-U0
says to include 0 lines of context around the changed lines--ie: include just the changed lines themselves. See man git diff
.-E
for grep allows it to work with extended regular expressions$''
syntax apparently allows ANSI quoting, which properly interprets the ESC (escape, or 0x1b) character properly. See here.^
matches the beginning of the line, \e
matches the Escape char, which is the start of a color code in the terminal, \[
matches the next char in the color code, which is [
, and then the (this|that)
syntax matches "this" or "that", where "this" is 32m+
, which is a green + line, and 31m-
is a red - line. \e[32m
is green and \e[31m
is red.+
shows lines marked by git diff
as added, of course, and -
shows lines marked by git diff
as deleted.--color=never
is required in the 2nd grep
expression in order to prevent it from highlighting its matches, which would otherwise screw up the color codes coming in from git diff
to the left.+
has to be escaped too as \+
because otherwise the +
is a special regular expression (regex) character which specifies one or more occurrences of the preceding element. See here: https://en.wikipedia.org/wiki/Regular_expression#Basic_concepts. ./git-filechange-search.sh path/to/my/file.cpp variable_name
will find all commits with changes to file.cpp that contain variable_name
in them. This is useful to see where and when certain features were changed. It's as though it were a search that could observe sections of a file displayed via git blame
over time.