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: follow one function through history with git log -L

Git
~/tips/tip-git-log-function-history

git log -L :parseConfig:src/config.js

Shows only the commits that changed one function or line range in a file.

File history is often too wide. What I usually want is the history of one function.

git log -L :parseConfig:src/config.js

The -L option traces the evolution of a line range or a function inside a file. With :name:file git takes a regular expression for the function name and follows that function. You get a list of commits, each with only the patch that touched that function. The docs say it implies --patch.

You can also use line numbers, which works for anything that is not a function:

git log -L 40,60:src/config.js

Compare that with git log -p src/config.js, which shows every change to the whole file. For a big file that is a lot of scrolling past changes you do not care about.

A few caveats. You cannot add a path filter on top of -L, and it works from a single starting revision. Git finds function boundaries the same way it picks the hunk headers in a diff, so for a language git does not know, you may need to set a funcname pattern in gitattributes, or fall back to line numbers. And the function has to exist in the commit where you start.

Further reading: git log documentation, On this site: git commit –fixup plus –autosquash keeps your history clean without any editing, On this site: read a small edit in a long line with git diff –word-diff

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