Java .gitignore template
The Java template ignores compiled classes, packaged archives and build directories, while explicitly keeping the Gradle wrapper jar that a clone needs in order to build at all.
At a glance
- Category
- Language
- Patterns in this template
- 18 rules
- Directories ignored
- 3 of the 18 rules end in a slash, so they match directories only
- Re-include rules
- 3 lines start with ! to keep something back
- Also needs an OS template
- Yes, and an IDE template too. This one covers the compiler, not the tooling around it.
- Typical companions
- Gradle or Maven, plus IntelliJ IDEA or Eclipse and an OS template.
In detail
*.class is the compiled bytecode for each source file. It is produced by the compiler from the .java file next to it, it changes whenever the compiler version changes, and a committed one will shadow the source in confusing ways when the two drift apart. The build and .gradle directories hold the same idea at a larger scale, plus the incremental compilation state that only makes sense on the machine that produced it.
The archive rules cover the packaged outputs: jar, war, ear, nar. These are the deliverables, produced by the build from the sources, and a release process that publishes them to an artifact repository has no reason to also keep them in git. The same rules also catch zip, tar.gz and rar, which is where the template starts being broader than it looks.
The three negation lines are the interesting part. Ignoring *.jar would also ignore gradle/wrapper/gradle-wrapper.jar, and that file is not a build output at all: it is the small bootstrapper that lets somebody who has never installed Gradle run ./gradlew and get the exact version the project expects. The negation puts it back. The other two negations protect source directories that happen to contain a folder named build.
hs_err_pid and replay_pid files are crash dumps written by the virtual machine when it dies hard. They contain the full command line, environment and loaded module list of the machine that crashed, which makes them both large and mildly revealing, and they are only ever useful to the person debugging that specific crash.
*.ctxt and .mtj.tmp come from teaching and profiling tools respectively, and are harmless leftovers to keep in the list. What the template does not cover is a local configuration file holding a database password, which in Java projects tends to live in an application properties file under src/main/resources.
Examples
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
replay_pid*
.gradle/
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/18 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- build/ with a trailing slash matches directories only. Written as build, the rule would also match a file called build, which is why most templates keep the slash: it makes the intent explicit and avoids ignoring a script of that name.
- The *.zip and *.tar.gz rules are wide. A test fixture archive or a sample dataset checked into the repository will be silently ignored, and the failure only shows up when CI clones fresh and the test cannot find its input.
- If the wrapper jar was ignored before the negations existed, adding them does not resurrect it. Check with git status --ignored and add the file explicitly with git add -f if needed.
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 →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.
Gradle .gitignore template
Six rules covering the Gradle cache and build directories, with negations that keep the wrapper jar and properties tracked because a clone needs both.
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.
Go .gitignore template
The Go template ignores platform binaries by extension, which misses the extensionless binary go build produces on Linux and macOS. Here is what to add.
Rust .gitignore template
The Rust template ignores the target directory and leaves Cargo.lock tracked. Whether that is right depends on whether you ship a binary or a library.
C .gitignore template
What the C .gitignore template covers: object files, static and shared libraries, linker maps, debug symbols and the patterns that swallow a vendored binary.
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