TOOL » GIT

Grep

Do not break lines on long lines matches (minified scripts!).

shell
git config --global core.pager 'less -XFS'

Usage

shell
git grep OPTIONS PATTERN
OptionDescription
-IDon’t match the pattern in binary files.
-l --files-with-matchesShow only the names of files that contain matches.
-i --ignore-caseIgnore case differences between the patterns and the files.
-E --extended-regexpExtended regexp (default).
-F --fixed-stringsUse fixed strings for patterns (don’t interpret pattern as a regex).

Examples

Match with filter for files:

shell
git grep 'some text' -- '*.[ch]'  # Only on .c or .h files

Search through all the history:

shell
git rev-list --all | xargs git grep 'some text'

Find and replace all matches recursively in files:

shell
git grep --files-with-matches foo | xargs -n 1 sed -i 's/foo/boo/g'