Git add lines to index by grep/regex

patchutils has a command grepdiff that can be use to achieve this.

# check that the regex search correctly matches the changes you want.
git diff -U0 | grepdiff 'regex search' --output-matching=hunk  

# then apply the changes to the index
git diff -U0 | grepdiff 'regex search' --output-matching=hunk | git apply --cached --unidiff-zero 

I use -U0 on the diff to avoid getting unrelated changes. You might want to adjust this value to suite your situation.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)