Sub topic 7
Matching at any depth with **
This chapter assumes you know the basic Git workflow (git init, git add, git commit, git status) and the pattern basics from earlier chapters: *, ?, trailing / for directories, and anchoring with a leading /. Here you will learn how the double asterisk ** lets one rule match across directory levels.
Why a single * is not enough
A single * never crosses a slash. The pattern docs/*/cache matches docs/api/cache, but it does not match docs/api/v2/cache. A pattern with a slash in the beginning or middle is also anchored to the location of the .gitignore file. This makes it hard to say "this directory, wherever it appears."
Git gives ** special meaning only when it is a whole path segment, that is, when it sits between slashes or at the start or end of the pattern. There are three forms.
| Form | Example | Meaning |
|---|---|---|
| Leading `**/` | `**/temp/` | Matches in all directories |
| Middle `/**/` | `docs/**/.cache/` | Matches zero or more directories between the two parts |
| Trailing `/**` | `build/**` | Matches everything inside `build`, at any depth |
The three forms in practice
# Any directory named temp, at any depth
**/temp/
# Any .cache directory anywhere under docs
docs/**/.cache/The first rule ignores temp/, src/temp/ and src/lib/temp/. The second rule ignores docs/.cache/, docs/api/.cache/ and docs/api/v2/.cache/. Because the middle form matches zero or more directories, the first of those paths matches too.
A leading **/ followed by a plain name is equivalent to writing the name with no slash at all. A pattern with no slash, such as temp, already matches at any depth, so **/temp and temp behave the same. The **/ form is mainly useful for readability, or when you need the pattern to contain a slash for another reason, such as **/foo/bar, which matches a bar directly inside any directory named foo.
The trailing form needs care. build/** matches everything inside build, but not build itself. Because the files inside are matched individually, this form also works with negation, which the chapter on re-including files covers.
How the matching flows
Common errors
Gluing ` to other characters.** In src/.log the double asterisk is not a whole segment, so Git treats it like an ordinary *. It will not cross directory levels. To ignore all .log files at any depth under src, write src//.log. Often the simpler .log` is enough, since a pattern without a slash already matches at any depth.
Expecting ` to override an ignored parent.** Matching deeply does not change the parent directory rule. If **/logs/ ignores a directory, no file inside it can be re-included with !`. Git does not look inside an ignored directory.
Forgetting the anchor. docs/**/.cache/ only applies under the docs directory next to the .gitignore file that holds the rule. A .cache directory under src is not affected.
Try it
You can check the behavior with a throwaway repository.
git init demo && cd demo
mkdir -p docs/api/v2/.cache src/temp
touch docs/api/v2/.cache/data src/temp/x.txt
printf '**/temp/\ndocs/**/.cache/\n' > .gitignore
git check-ignore -v docs/api/v2/.cache/data src/temp/x.txtEach path is reported with the matching line of .gitignore: line 2 for the cache file and line 1 for the file in temp.
Exercise
You want to ignore every directory named generated at any depth, and also every .tmp file anywhere under app/, including app/a/b/c.tmp. Write the two .gitignore lines.
Show answer
**/generated/ (or simply generated/, since a pattern with only a trailing slash matches at any depth) and app/**/*.tmp. The second line uses the middle /**/ form so that it matches zero or more directories between app/ and the file name.
Exercise
Why does src/**.log fail to ignore src/a/b/debug.log, and how do you fix it?
Show answer
The ** is attached to other characters, so it is not a whole path segment and Git treats it like a single *, which does not cross slashes. Write src/**/*.log instead.