Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git add lines to index by grep/regex

Tags:

git

git-add

regex

I have a giant patch that I would like to break into multiple logical git commits. A large number of the changes are simply changing variable names or function calls, such that they could easily be located with a grep. If I could add to the index any changes that match a regex then clean up in git gui, it would save me a lot of manual work. Is there a good way to update the index on a line-by-line basis using a regex within git or from some output of grep (e.g. line numbers)?

I found a similar question, but I'm not sure how to build the temporary file from a regex-type search.

like image 618
FazJaxton Avatar asked Mar 05 '13 21:03

FazJaxton


1 Answers

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.

like image 181
Gary van der Merwe Avatar answered Sep 24 '22 16:09

Gary van der Merwe