01 / 21

Using .gitignore

Learn how to exclude build artifacts and unnecessary files from your Git repository using .gitignore patterns in Java projects.

Java Git & Version Control

What is .gitignore?

A .gitignore file is a plain text file placed in your repository root that tells Git which files and directories to intentionally ignore. Any file or folder matching a pattern in .gitignore will not be tracked, staged, or committed.

Why Ignore Files?

★ Smaller Repos
Build outputs like /target can be hundreds of megabytes. Ignoring them keeps your repo lean and fast to clone.
★ Cleaner History
Auto-generated files create noisy diffs. Ignoring them means commits only show meaningful changes.
★ Security
Config files with passwords or API keys should never be committed. A .gitignore prevents accidental leaks.

Build Artifacts in Java Projects

Source Code Build Tool Maven / Gradle /target or /build compiled .class, .jar ✕ Ignore src/ Main.java Utils.java pom.xml .gitignore README.md target/ ← excluded Tracked Ignored Source

The Core Rule

✓ Track
Source code, config files, documentation, build scripts (pom.xml, build.gradle)
✕ Ignore
Compiled .class files, /target, /build, .jar outputs, IDE metadata, OS files

Pattern Syntax

*.class Wildcard: ignores all files ending in .class /target/ Leading slash: only the root-level target directory **/build/ Double star: build/ at any depth in the tree !important.log Negation: re-include a file that was ignored # Comment Lines starting with # are comments (ignored by Git)

Tracked vs Untracked Files

.gitignore only affects untracked files. If a file is already being tracked by Git (was previously committed), adding it to .gitignore will not remove it. You must explicitly untrack it first.

Untracked File Already Tracked File
Add to .gitignore ✓ Immediately ignored ✕ Still tracked
Appears in git status No Yes (shows changes)
Fix if already tracked N/A git rm --cached

How to Set Up .gitignore

1
Create the file in your project root. Name it exactly .gitignore (leading dot, no extension).
2
Add patterns for files and directories to exclude: build outputs, IDE files, OS files, and secrets.
3
Commit .gitignore itself. The file should be tracked so all collaborators share the same ignore rules.
4
Verify with git status: ignored files should no longer appear as untracked.
5
Clean up any previously tracked artifacts using git rm --cached if needed.

Example: Maven Project

# Build output /target/ # Compiled class files *.class # Package files *.jar *.war # IDE files .idea/ *.iml .vscode/ # OS files .DS_Store Thumbs.db

Example: Gradle Project

# Gradle build output /build/ # Gradle wrapper (optional to ignore) .gradle/ # Compiled class files *.class # IDE files .idea/ *.iml # Logs *.log # Environment variables .env

Advanced Patterns

# Ignore all .log files everywhere **/*.log # Ignore build dirs at any depth **/build/ **/target/ # Ignore all .class except test helpers *.class !src/test/helpers/*.class # Ignore everything in config/ except template config/* !config/template.properties

How Git Processes Patterns

New/Modified File Check .gitignore patterns (top to bottom) No match Match File is Tracked File is Ignored Shows in git status Invisible to Git

Common Java Artifacts to Ignore

/target/
Maven build output: compiled .class files, packaged .jar/.war, test reports
/build/
Gradle build output: same type of artifacts, different directory name
.idea/ & *.iml
IntelliJ IDEA project settings: personal to each developer's environment
.gradle/
Gradle's local cache directory: automatically regenerated on build

Global vs Local .gitignore

Global (~/.gitignore_global) Local (.gitignore)
Scope All repos on your machine This repo only
Shared with team? ✕ No (personal config) ✓ Yes (committed)
Best for OS files (.DS_Store, Thumbs.db) Build artifacts (/target, /build)
Setup git config --global core.excludesfile Create file in project root

Common Mistakes

✕ Ignoring After Commit
Adding /target/ to .gitignore after it's already been committed. Git keeps tracking it. Fix: git rm --cached -r target/
✕ Wrong File Name
Naming it gitignore (no dot) or .gitignore.txt. The file must be exactly .gitignore with no extension.
✕ Negation Before Rule
Placing !keep.log before *.log. Negations only work when they appear after the rule they override.

Pattern Reference Summary

Pattern Meaning Example Match
*.class All .class files in any directory src/Main.class
/target/ Root-level target directory only target/app.jar
**/build/ build/ at any depth module/build/
*.log All .log files debug.log
!important.log Re-include despite *.log important.log
config/ Any directory named config src/config/

Question 1 of 5

What is the primary purpose of a .gitignore file?

Question 2 of 5

In a Maven Java project, which directory contains compiled build artifacts and should be added to .gitignore?

Question 3 of 5

You already committed the /target/ directory, then added it to .gitignore. What happens?

Question 4 of 5

What does the pattern **/build/ match in a .gitignore file?

Question 5 of 5

Given this .gitignore content, which file will not be ignored?