A Gradle .gitignore ignores build/ in every project, the .gradle/ cache folder, and stray *.class and *.jar files, then re-includes gradle/wrapper/gradle-wrapper.jar so the wrapper survives a clean checkout. Those are the key lines of the tool’s Java (Gradle) template, which has 14 in all.
What the blocks ignore
- Build output:
build/, at any depth, soapp/build/andbuildSrc/build/go too.!**/src/**/build/follows it, because a package such ascom.acme.buildlives in a source folder calledbuild, and Gradle writes its own output outsidesrc/. - Caches:
.gradle/, the project’s cache and daemon state, rebuilt on the next run. - Compiled files:
*.class,*.jar,*.war,*.ear, plus JVM crash logs and*.hprofheap dumps. - IDE helpers:
gradle-app.settingand.gradletasknamecache.
In a scratch repository with this file, git check-ignore -v -n --no-index names the line that matched each path, or prints :: when none did:
.gitignore:10:build/ app/build/classes/java/main/App.class
.gitignore:10:build/ buildSrc/build/libs/buildSrc.jar
.gitignore:12:!**/src/**/build/ app/src/main/java/com/acme/build
:: app/src/main/java/com/acme/build/Tool.java
.gitignore:14:.gradle/ .gradle/8.10/fileHashes/x
.gitignore:19:!gradle/wrapper/gradle-wrapper.jar gradle/wrapper/gradle-wrapper.jar
:: gradlew
:: gradle/libs.versions.toml
.gitignore:3:*.jar libs/legacy.jar
Commit the wrapper, all four files
Gradle’s documentation says the wrapper files belong in version control: gradlew, gradlew.bat, gradle/wrapper/gradle-wrapper.properties and gradle/wrapper/gradle-wrapper.jar. Ignoring that jar is the classic mistake, because *.jar matches it; without it, ./gradlew fails on a fresh clone. Commit gradle/libs.versions.toml and settings.gradle.kts too.
The ! line has to come last
The last matching line wins, so the re-include must follow *.jar. The same two lines in the other order leave the jar ignored:
.gitignore:3:*.jar gradle/wrapper/gradle-wrapper.jar
It also needs its folder to be visible: after a line like gradle/, Git never looks inside, and no ! line below can bring the jar back.
Kotlin and Android
Kotlin 2 writes a .kotlin/ folder at the root, which this template doesn’t know (:: .kotlin/sessions/x above); add the Kotlin template. Android projects also need local.properties and *.apk ignored, which the Android template covers.
Already committed build/ or .gradle/?
.gitignore only affects untracked files. Untrack what was committed, keeping it on disk, and commit:
git rm -r --cached build app/build .gradle
git commit -m "Stop tracking Gradle output"
Editor and OS files
.idea/ per-user files and .DS_Store come from your machine, not from Gradle. Pick the JetBrains template, or put them in a global ignore file:
git config --global core.excludesFile ~/.gitignore_global