gitmsr

How to search Git commit messages with regular expression and output those messages & their line number to a text file


How to search Git commit messages (not diffs) in a software repository with regular expression and output those messages & their line number to a text file?


Solution

  • You can 'grep' through commit log messages by doing the following:

    git log -E --grep="regex" --oneline >/tmp/results.txt
    

    This will result in e.g.:

    abcd1234 First commit containing regex word. defg5679 Another commit: regex is found here.

    I'm not sure what you mean by line number - if you just want every line in the file numbered, you can achieve this by piping the results through nl:

    git log -E --grep "foo" | nl -w 1 -s ' ' > /tmp/results.txt 
    

    (-w 1 left-aligns numbers, and -s ' ' puts a single space after the number before the text).