Sub topic 4
Matching Directories and Anchoring Patterns with /
This chapter assumes you know the basic Git workflow (git init, git add, git commit, git status) and can move around a terminal. It also assumes you have seen simple patterns such as *.log and .env. Here you will learn how the slash character changes what a pattern matches.
The slash has three jobs
In a .gitignore file, a / can appear at the end, at the start, or in the middle of a pattern. Each position has a different meaning.
| Position | Example | Meaning |
|---|---|---|
| Trailing | `node_modules/` | Matches only directories, at any depth |
| Leading | `/dist/` | Anchors the pattern to the directory that holds the `.gitignore` |
| Middle | `docs/build` | Also anchors the pattern to that directory |
A pattern with no slash at all, such as *.log, is matched against the name of every file and directory at any depth below the .gitignore file.
Trailing slash: directories only
A pattern that ends with / matches only directories. Git then ignores the directory and everything inside it.
node_modules/This ignores a directory named node_modules anywhere in the repository. It does not ignore a regular file that happens to be called node_modules. Without the trailing slash, the pattern node_modules would match both files and directories with that name.
Use the trailing slash when you mean a directory. It documents your intent and avoids hiding a file by accident.
Leading slash: anchoring to the root
Without anchoring, a pattern matches at any depth. Suppose your project has a dist directory at the top and another one inside a package:
dist/
packages/app/dist/The pattern dist/ ignores both. To ignore only the top-level one, anchor it with a leading slash:
# Build output at the repository root only
/dist/Now packages/app/dist/ is not ignored by this rule. The slash is a path separator and is not part of the name, so /dist/ means "the dist directory directly inside the folder that contains this .gitignore."
Middle slash: also anchors
A slash anywhere except the end makes the pattern relative to the .gitignore file's own directory. This surprises many people.
docs/buildThis matches docs/build at the top of the repository. It does not match packages/app/docs/build. Wildcards also do not cross slashes, so docs/*.md matches docs/guide.md but not docs/api/guide.md. If you need to match across levels, use the double asterisk covered in a later chapter.
Seeing the difference
The next commands create the same file in two places and then test which one is ignored.
mkdir -p build packages/app/build
touch build/out.txt packages/app/build/out.txt
echo "/build/" > .gitignore
git status --shortThe output lists .gitignore and packages/ as untracked. The top-level build/ is hidden. If you change the file to contain build/ with no leading slash, both directories disappear from the output.
Common mistakes
- Expecting
docs/buildto match at any depth. The middle slash anchors it. Use**/docs/buildfor any depth. - Forgetting the trailing slash.
buildalso hides a file namedbuild. Writebuild/if you only mean the directory. - Anchoring in a nested file. In
packages/app/.gitignore, the pattern/dist/refers topackages/app/dist/, not the repository root. - Windows backslashes. Always use forward slashes, even on Windows.
Exercise
Exercise
A repository has tmp/ at the top level and src/tmp/ inside the source folder. You want to ignore only the top-level tmp/ directory. Which pattern should you add to the root .gitignore, and why does plain tmp/ not do the job?
Show answer
Add /tmp/. The leading slash anchors the pattern to the root, so only the top-level tmp/ is ignored. Plain tmp/ has only a trailing slash, which limits it to directories but still lets it match at any depth, so src/tmp/ would be ignored too.
Exercise
Does the pattern assets/raw in the root .gitignore ignore web/assets/raw? Does it ignore a file named raw inside assets/?
Show answer
It does not ignore web/assets/raw, because the slash in the middle anchors the pattern to the root. It does ignore assets/raw whether that is a file or a directory, because there is no trailing slash. A file named raw inside assets/ is exactly that path, so it is ignored.