⚡ TL;DR
You can use negative filters like
git diff ':!*.lock'
git status ':!*.lock'
and on attributes:
git status ':(attr:!linguist-generated !linguist-vendored)'
You can even combine those patterns.
Excluding on a glob
I sometimes have many changes over multiple files in a Git repository. And then I want to display only the changes made to files in the src/ directory:
git diff '*/src/*'
Or, more interesting, I want all changes except those made to lock files:
git diff ':!*.lock'
Note that in the above examples, patterns are quoted, so that the shell does not expand the glob itself. Here, Git is interpreting the patterns1. This feature is called “pathspec”.
Diving into pathspec
That feature can be used with commands like git ls-files, git status, git grep…
And you can do more than exclude globs.
The above :!*.lock is actually a short form of :(exclude)*.lock. And quite a few other keywords exist, like top to force a match on the full path from the root of the repository, icase for a case insensitive match or attr to place particular constraints on the attributes of a file.
For example, you can use the linguist attributes understood by GitHub and other forges locally with:
':(attr:!linguist-generated !linguist-vendored)'matches files that have neither of these linguist attributes,':(attr:linguist-generated)' ':(attr:linguist-vendored)'matches files with either attribute2,- and that can be combined with file patterns and other attributes, like
':(icase,attr:linguist-vendored)*/LOCK'which would match a file namedlockorLockor evenLoCkif it has thelinguist-vendoredattribute.
The manual contains even more details, like attributes gotchas, matching without interpreting globs…. Have fun!
Although, we could remove the quotes and just use the shell in the first example. It’s likely to be slower because the shell would consider all files, while Git can use gitignore, look for changed files first… ↩︎
Usually files don’t have both attributes, but if you wanted to match files with both attributes set, you could use
':(attr:linguist-generated linguist-vendored)'. ↩︎
Liked this post? Subscribe:
Discussions
This blog does not host comments, but you can reply via email, share on Lobsters, post on HackerNews or participate in one of the discussions below: