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 log and diff can leave a folder out with an exclude pathspec

Git
~/tips/tip-git-pathspec-exclude-folder

git diff main -- . ':(exclude)vendor'

Hides vendored or generated folders from log and diff with a pathspec.

Lock files and vendored libraries make diffs unreadable. Git paths accept a magic prefix that excludes them.

git diff main -- . ':(exclude)vendor'

The . means everything, and :(exclude)vendor takes the vendor folder back out. There’s a short form that does the same, ':!vendor', but in bash and zsh the exclamation mark can trigger history expansion, so keep it in single quotes. You can stack as many exclusions as you like:

git log --oneline -- . ':!vendor' ':!package-lock.json'
git diff --stat main...HEAD -- . ':(exclude)*.min.js'

This works for every command that takes a pathspec: git log, git diff, git grep, git add and more. It helps most with git diff --stat on a pull request where half the line count is a regenerated lock file.

Watch where you run it. Pathspecs are relative to your current folder, so . in a subfolder means only that subfolder. Add :/ to start from the top of the repository, as in ':/' ':!vendor'. If you give only an exclusion and nothing else, git applies it to the whole repository, not just your folder, which I think is easy to trip over.

Further reading: gitglossary: pathspec

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