Imagine you have created a Java project and initialized it with Git.
After writing some code, you run:
git status
Instead of seeing only your source code files, Git displays dozens or even hundreds of files.
Untracked files:
.class files
.idea/
.vscode/
out/
logs/
temp/
You don't want these files to be stored in your Git repository.
They are:
Temporary files
Build output
IDE configuration
Cache files
Log files
Secret configuration files
Tracking these files would make your repository larger, messier, and less secure.
This is exactly why Git provides the .gitignore file.
The .gitignore file tells Git:
"Ignore these files and folders. Don't show them as untracked, and don't include them in future commits."
In this article, you will learn:
What
.gitignoreisWhy every project needs it
How it works
Ignoring files
Ignoring folders
Wildcards and patterns
Ignoring file extensions
Ignoring everything except certain files
Stopping Git from tracking files
Common mistakes
Best practices
By the end of this article, you'll know how to keep your Git repository clean, organized, and secure.
What is .gitignore?
A .gitignore file is a plain text file placed in the root of a Git repository.
It contains rules that tell Git which files and folders should be ignored.
Example:
*.class
logs/
temp/
Git ignores every file that matches these rules.
Why Do We Need .gitignore?
Suppose your Java project looks like this:
StudentManagement
│
├── src/
│ ├── Main.java
│ └── Student.java
│
├── out/
│
├── .idea/
│
├── logs/
│
└── README.md
Which files should Git store?
Definitely:
Source code
Documentation
But should Git store:
Compiled
.classfiles?IDE settings?
Temporary logs?
Usually, no.
These files are either generated automatically or specific to one developer's computer.
Ignoring them keeps the repository clean.
Creating a .gitignore File
Create a file named:
.gitignore
Notice something unusual.
The filename begins with a dot (.).
On many operating systems, files beginning with a dot are hidden by default.
Open the file in any text editor.
Now add:
*.class
Save the file.
Git will now ignore every .class file that is not already being tracked.
Ignoring One File
Suppose your project contains:
config.properties
You don't want Git to track it.
Add this to .gitignore:
config.properties
Git ignores that specific file.
Ignoring Multiple Files
Example:
secret.txt
passwords.txt
notes.txt
.gitignore
secret.txt
passwords.txt
notes.txt
Each filename appears on its own line.
Ignoring File Extensions
Suppose you want to ignore every compiled Java class.
Write:
*.class
This means:
Ignore every file ending with:
.class
Examples:
Main.class
Student.class
Teacher.class
All are ignored.
Common File Extension Patterns
*.class
*.log
*.tmp
*.bak
*.zip
Meaning:
| Pattern | Ignores |
|---|---|
*.class | Java compiled files |
*.log | Log files |
*.tmp | Temporary files |
*.bak | Backup files |
*.zip | ZIP archives |
Ignoring a Folder
Suppose your project contains:
logs/
Ignore it:
logs/
Git ignores the folder and everything inside it.
Example:
logs/
logs/error.log
logs/debug.log
logs/server.log
All are ignored.
Ignoring Multiple Folders
logs/
temp/
cache/
out/
Git ignores every listed directory.
Ignoring Build Output
Java projects often generate:
out/
bin/
target/
build/
These folders are usually created automatically.
Example:
out/
Git ignores every file inside the folder.
Ignoring IDE Files
Developers use different IDEs.
Examples:
.idea/
.vscode/
.settings/
These folders store editor preferences.
They usually should not be committed because they vary from one developer to another.
Ignoring Operating System Files
Some operating systems automatically create hidden files.
Examples:
macOS:
.DS_Store
Windows:
Thumbs.db
These files are not part of your project.
Add:
.DS_Store
Thumbs.db
to .gitignore.
Understanding Wildcards
Git supports pattern matching.
The most common wildcard is:
*
It means:
Any sequence of characters
Example:
*.txt
Matches:
notes.txt
todo.txt
hello.txt
Using the Question Mark (?)
Example:
file?.txt
Matches:
file1.txt
file2.txt
fileA.txt
Does not match:
file10.txt
because ? represents exactly one character.
Ignoring an Entire File Type
Suppose your repository contains:
report.pdf
notes.pdf
book.pdf
Ignore them:
*.pdf
Git ignores every PDF file.
Ignoring Everything Inside a Folder
Suppose:
uploads/
contains:
uploads/
image1.png
image2.png
video.mp4
Use:
uploads/
Everything inside the folder is ignored.
Ignoring Only the Contents
Suppose you want Git to keep the folder but ignore its contents.
Example:
logs/*
This ignores the files inside logs/.
The folder itself can still exist in the repository if it contains tracked files or special placeholder files.
Negation (!)
Sometimes you want to ignore everything except one file.
Example:
logs/
!logs/.gitkeep
Git ignores everything inside logs/ except:
.gitkeep
This technique is commonly used to keep empty directories in a repository.
Does .gitignore Affect Tracked Files?
This is one of the biggest beginner misunderstandings.
Suppose:
password.txt
has already been committed.
Now you add:
password.txt
to .gitignore.
Will Git stop tracking it?
No.
.gitignore only affects untracked files.
Tracked files remain tracked.
Stopping Git from Tracking a File
Suppose:
.env
has already been committed.
Now you want Git to ignore it.
Run:
git rm --cached .env
This removes the file from Git's index while leaving it on your computer.
Now add:
.env
to:
.gitignore
Commit the change.
From now on, Git will ignore future modifications to that file unless you intentionally track it again.
Real-World Example
Suppose your Java project contains:
StudentManagement
│
├── src/
├── out/
├── logs/
├── .idea/
├── README.md
├── Main.java
└── Main.class
A good .gitignore file would be:
*.class
out/
logs/
.idea/
*.log
Now Git tracks only:
Java source code
README
Project files
Everything else remains ignored.
Common .gitignore for Java Projects
# Compiled Java files
*.class
# Build folders
bin/
out/
target/
build/
# IDE folders
.idea/
.vscode/
.settings/
# Logs
*.log
# Temporary files
*.tmp
# macOS
.DS_Store
# Windows
Thumbs.db
This is a good starting point for many Java projects, though the exact entries may vary depending on your build tools and IDE.
Checking Ignored Files
Suppose you're unsure why a file is being ignored.
You can run:
git check-ignore -v filename
Example:
git check-ignore -v Main.class
Git shows the matching .gitignore rule that caused the file to be ignored.
This is very useful when troubleshooting.
Common Beginner Mistakes
Mistake 1
Adding .gitignore after committing unwanted files.
Remember:
.gitignore does not automatically stop tracking files that are already committed.
Mistake 2
Ignoring important source code files.
Always review your patterns carefully.
For example:
*.java
would ignore every Java source file.
That is almost certainly not what you want.
Mistake 3
Ignoring secrets after they have already been pushed.
Adding:
.env
to .gitignore does not remove secret data from previous commits.
If sensitive information has already been committed and shared, additional steps are needed to remove it from history and rotate the exposed secrets.
Mistake 4
Using very broad wildcard patterns.
Patterns like:
*
can ignore far more files than intended.
Best Practices
Create a
.gitignorefile at the beginning of every project.Ignore build output and temporary files.
Ignore IDE-specific folders.
Never commit passwords, API keys, or secret configuration files.
Review ignored patterns before committing.
Use
git check-ignore -vwhen debugging ignore rules.Use project-specific
.gitignoretemplates as a starting point and customize them as needed.
Useful .gitignore Patterns
| Pattern | Purpose |
|---|---|
*.class | Ignore Java compiled files |
*.log | Ignore log files |
*.tmp | Ignore temporary files |
logs/ | Ignore an entire folder |
build/ | Ignore build output |
.idea/ | Ignore IntelliJ IDEA settings |
.vscode/ | Ignore VS Code settings |
.env | Ignore environment configuration |
!important.txt | Do not ignore this specific file |
Summary
The .gitignore file is one of the most important files in any Git repository. It helps keep repositories clean by preventing unnecessary, temporary, generated, or sensitive files from being tracked.
By understanding ignore patterns, wildcards, directory rules, and the difference between tracked and untracked files, you can build repositories that are easier to manage, collaborate on, and secure.
A well-written .gitignore file is a small investment that saves countless hours of cleanup and prevents accidental commits of unwanted files.
Comments
Post a Comment