Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see changed lines with certain words and the containing file for a git commit? - Can git diff print a file name line prefix?

Before pushing a Git commit, I usually want to review all TODOs that I've added in the code. I've tried several approaches, but I'm still haven't found a good way to do this.

E.g. with the following command, I can see all TODOs added (and removed) in the last commit:

git diff HEAD^ | grep "TODO"

However in this output I don't see which files contained the change so I have to guess or search the corresponding files. With the following command, I see all files with a "TODO" change, but I don't see the actual TODOs:

git diff HEAD^ --name-only -G "TODO"

So my question is how can I both see the diff line and the name of the file containing the difference? Is it e.g. possible to make git diff prefix every diff line with the file name? Then the grep'ed lines would give have all the information I need.

like image 718
oberlies Avatar asked Dec 23 '14 15:12

oberlies


1 Answers

The following is getting close to what I originally wanted:

git diff HEAD^ --name-only -G "TODO" | xargs git grep "TODO" --

This prints all TODOs in the files touched in the last commit - and not just the TODOs that were added.

But this is also quite useful. In this way I can also check if there may be other TODOs that can now be removed.

like image 177
oberlies Avatar answered Sep 23 '22 19:09

oberlies