How to see modified lines with specific words and containing a file for git commit? - Can git diff print the file name string prefix?

Before pushing a Git commit, I usually want to see all the TODOs that I added in the code. I tried several approaches, but I still have not found a good way to do this.

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

git diff HEAD^ | grep "TODO"

However, in this release, I don’t see which files contained the changes, so I have to guess or search for the corresponding files. With the following command, I see all the files with the change "TODO", but I do not see the real TODO:

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

So my question is: how can I see the diff line and the name of the file containing the difference? For example, is it possible to git diffprefix each line with a file name? Then the grep'ed lines will provide all the information I need.

+4
source share
1 answer

The following comes close to what I originally wanted:

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

This prints all the TODOs in the files affected by the last commit, not just the ones added by TODOs.

But it is also very useful. That way, I can also check if there can be other TODOs that can now be deleted.

+1
source

Source: https://habr.com/ru/post/1568774/


All Articles