Yarn .gitignore template
The Yarn template ignores the contents of the tool's directory while keeping five subdirectories tracked, and its correctness depends on whether you use zero install mode.
At a glance
- Category
- Build Tool
- Patterns in this template
- 10 rules
- Directories ignored
- 1 of the 10 rules end in a slash, so they match directories only
- Re-include rules
- 5 lines start with ! to keep something back
- Also needs an OS template
- Yes. This covers the package manager only.
- Typical companions
- JavaScript or Node.js, plus an OS template and an editor template.
In detail
The .yarn/* rule with five negations is the same construction the VS Code template uses, and for the same reason. Ignoring the directory outright would stop git descending into it, so no negation below could work. Ignoring the contents keeps the directory visible and lets the exceptions apply.
The five kept directories are the ones that are genuinely part of the project. patches holds patch files applied to dependencies after install, which are source and would be lost forever if untracked. plugins holds tool extensions the project depends on. releases holds a pinned copy of the package manager itself, so everyone runs the same version. sdks holds editor integration shims, and versions holds the release workflow's version bump files.
What is not listed is the cache, and that is where the judgement call sits. The zero install workflow deliberately commits .yarn/cache, so a clone can install with no network access at all and CI needs no install step. Teams that use it accept a larger repository in exchange for reproducibility and speed. Teams that do not want the size ignore the cache and install normally.
.pnp.* covers the Plug'n'Play resolution files, which replace the node_modules directory entirely by mapping every import to a location inside the cache. Whether they should be ignored follows the same decision: in a zero install setup they are committed alongside the cache, because they are what makes the cache usable.
node_modules is kept in the list because a project can switch resolution modes, and the debug and error log rules cover the files written when an install fails. The lockfile itself, yarn.lock, is deliberately absent from every rule here, which is correct: it must be committed.
Examples
node_modules/
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
yarn-debug.log*
yarn-error.log*10 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- If you use zero install mode, you must also track .yarn/cache and .pnp.*, which means removing rules rather than adding them. This template assumes the ordinary mode.
- .yarn/patches must stay tracked. A patch file is source you wrote, and losing it means the dependency fix silently stops applying on the next clean install.
- The negations only work because the rule is .yarn/* and not .yarn/. Changing it to the directory form disables all five exceptions at once.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →npm .gitignore template
This template ignores package-lock.json, which contradicts the current advice to commit the lockfile. Here is what the rest of it covers and what to change.
pnpm .gitignore template
The shortest package manager template, because pnpm keeps its content addressed store outside the project and links into it rather than copying packages.
Node.js .gitignore template
The longest JavaScript oriented template: dependency directories, every package manager's debug log, bundler caches, env files and Yarn Plug'n'Play state.
Docker .gitignore template
A small template with two rules worth questioning: it ignores .dockerignore, and it ignores every compose file except two, which suits few projects.
Terraform .gitignore template
State files contain secrets in plain text and must never be committed. This template covers that, and also ignores the lock file, which should be tracked.
Vagrant .gitignore template
Two rules worth understanding: the .vagrant directory holds machine identifiers and a generated private key, and box files are multi gigabyte disk images.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- package.json Generator Generate package.json files with a form-based builder - 8 project presets, all fields supported