brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git grep -p shows which function a match lives in

Git
~/tips/tip-git-grep-function-context

git grep -n -p TODO

Searches only the files git tracks and names the function around every match.

I reach for git grep before plain grep inside a repository. By default it looks only at tracked files in the working tree, so node_modules, build output and other junk stay out of the results without a single exclude flag.

git grep -n -p TODO

-n adds line numbers. -p (--show-function) is the nice one: above each match git prints the line with the name of the function it sits in, so you see straight away that this TODO is in parse_config and not in some test helper. -W goes further and prints the whole function around the match. Add -i to ignore case.

It can search old versions too, without a checkout. Put a tag or commit after the pattern:

git grep -n "max_retries" v1.0

That searches the files as they were in v1.0. I use that kind of check to see when a setting was still called something else.

Caveat: new files you haven’t added yet are invisible to it, because they’re not tracked. Add --untracked to include them. The docs note that this flag can’t be combined with --recurse-submodules.

Further reading: git-grep documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link