Gradle .gitignore template
The Gradle template ignores the build directory and the tool's local cache, and negates the two wrapper files that must stay tracked for a fresh clone to build.
At a glance
- Category
- Build Tool
- Patterns in this template
- 6 rules
- Directories ignored
- 2 of the 6 rules end in a slash, so they match directories only
- Re-include rules
- 4 lines start with ! to keep something back
- Also needs an OS template
- Yes, along with an IDE template. This one covers the build tool only.
- Typical companions
- Java or Kotlin, plus IntelliJ IDEA or Android Studio and an OS template.
In detail
.gradle is the build tool's project local cache. It holds file hashes for up to date checks, the configuration cache, daemon registry entries and task history. All of it is keyed to absolute paths on this machine, so a copy from someone else's checkout produces cache misses at best. Deleting it costs one slower build.
build is the output directory, and because the rule is unanchored it covers every module in a multi project build without listing them. That is convenient and it is also why the next two negations exist. The same convenience applies to any directory named build anywhere in the tree, whether or not the build tool created it, which is the trade the unanchored form makes.
The wrapper negations are the point of this template. The wrapper is a small script plus a jar plus a properties file, committed on purpose so that anyone who clones the repository can run ./gradlew and get the exact build tool version the project expects, without installing anything. Ignoring *.jar in a Java project would take the jar with it, so the negation puts it back. The properties file records which version to fetch and its checksum.
The two source directory negations exist because the build rule matches at any depth. A source tree containing a package named build would otherwise be ignored, which is a silent and confusing failure, so the template protects the main and test source trees explicitly.
Committing a binary jar makes some people uncomfortable, and it is worth knowing why the practice is recommended anyway: the wrapper properties file carries a checksum, so a clone can verify the jar it has against the version the project declared, and the jar itself only downloads and launches the real distribution.
Examples
.gradle/
build/
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties
!**/src/main/**/build/
!**/src/test/**/build/6 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- Ignoring the wrapper jar breaks a fresh clone for anyone without the matching build tool version installed. The negation is not optional tidiness.
- build/ is unanchored, which is what makes multi module projects work, and also what makes the two source tree negations necessary.
- A negation cannot rescue a file inside an already ignored directory. These negations work because build/ is the ignored directory and the negated paths are outside it.
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 →Java .gitignore template
What the Java .gitignore template ignores: compiled classes, packaged archives, crash dumps, and the negations that keep the Gradle wrapper jar tracked.
Kotlin .gitignore template
The Kotlin template covers the same class and archive output as Java, plus the .kotlin session directory the newer compiler writes into the project root.
Maven .gitignore template
The Maven template ignores the target directory and the backup files the release plugin leaves behind, including the wrapper jar that Gradle keeps.
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.
Yarn .gitignore template
The Yarn template ignores the .yarn directory's contents while negating five subdirectories, because which ones you keep depends on the install mode you use.
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.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- GitHub Actions Generator Free online github actions generator - generate CI/CD workflow YAML files from templates