What Is Gitignore? How to Write a .gitignore File, With Example Templates
Updated: October 13, 2026 · AracMerkezi
.gitignore is a text file that tells Git which files and folders it should not put under version control. It is used for things that should stay out of the repository, such as build output, dependency folders, environment files and editor settings.
Pattern rules
# comment: A comment line.*.log: All files with the.logextension.build/: A trailing slash matches directories only./config.local: A leading slash matches only a file in the same directory as the.gitignore.!important.log: An exclamation mark is an exception to an earlier rule.**/temp: Atempfolder at any depth.*,?and[a-z]are wildcard characters.
Rules are processed from top to bottom; a later rule can override an earlier one. If a folder itself is ignored, re-including a file inside it with ! does not work; first narrow the rule that ignores the folder.
Example for Node.js
node_modules/
dist/
.env
*.log
.DS_Store
Example for Python
__pycache__/
*.pyc
.venv/
.env
dist/
*.egg-info/
Example for Java
*.class
target/
.idea/
*.iml
Ready-made template
Instead of writing the right combination for your project by hand, you can use the .gitignore Generator to pick Node, Python, Java, operating system and editor templates and copy the merged file. Templates are a starting point; add your project's own output.
A file that is already tracked is not being ignored
.gitignore only affects files that are not yet tracked. If a file was added to the repository earlier, it stays tracked even if it later matches a rule. The fix:
git rm --cached file.txt
git rm -r --cached folder/
git commit -m "Untrack files"
The --cached option does not delete the file from your disk; it only removes it from Git's tracking.
Warning about secrets
Ignoring the .env file is a good step, but if you have pushed a password or an API key to the repository even once, the information remains in the history. Deleting the file afterward is not enough. In that case, revoke the key immediately and create a new one; cleaning the history is a separate and more complicated process. Share the sample configuration as .env.example, without real values.
Common mistakes
- Saving the file as
.gitignore.txt. - Assuming that a tracked file will be ignored.
- Leaving unnecessary trailing spaces at the end of a pattern.
- Ignoring lock files (such as
package-lock.json); these are usually committed to the repository. - Writing rules that belong in a global ignore (operating system files) into every repository; a user-level global setting can be used for this instead.
Step by step: setting up a new project
- Before starting the project, create the
.gitignorefile with suitable templates. - Use
git statusto check that unwanted files do not appear in the list. - Add example files that contain no real values, such as
.env.example. - Make the first commit only after this check.
Finding out why a rule does not work
The git check-ignore -v file.txt command shows which rule, in which file, ignores the file (or that it is not ignored). This way you can see whether the rule is misspelled, in the wrong directory, or overridden by a later rule.
Multiple .gitignore files
Subfolders can also contain a .gitignore file; its rules affect that folder and everything below it. In large repositories, it is a common technique for an output folder to carry its own .gitignore containing just *.
Commonly used example rules
*.env: Ignores all.envfiles./uploads/*and!/uploads/.gitkeep: Used to ignore uploaded files while keeping the empty folder.docs/**/*.pdf: Ignores all PDF files underdocs.!.vscode/settings.json: Defines an exception to keep the team settings in the repository (if the parent folder is not ignored separately).
User-specific ignores
For files that concern only your own computer (editor settings, operating system files), you can use the .git/info/exclude file, which is not part of the repository, or a user-level global ignore file. This way you do not clutter your teammates' repository with unnecessary lines.
Limits
Templates are meant for general use; adapt them to the framework and build tools you use. To see the effect of the file, use the git status command.
Frequently asked questions
I added the file to .gitignore but it is still tracked. Why?
The file was added to the repository earlier; you need to untrack it with the git rm --cached file command.
Is deleting a key I accidentally pushed to the repository enough?
No; the information remains in the history. Revoke the key and create a new one.
How do I ignore a folder?
Write the folder name with a trailing slash, for example node_modules/.
Tools mentioned in this guide
More guides
- How to Calculate Percentages: Six Question Types, Formulas and Examples – Percentage math: percent of a number, what percent one is of another, increase and decrease, percentage points and reverse percentages, with examples.
- How to Calculate VAT: VAT-Inclusive and VAT-Exclusive Formulas and Examples – How do you separate VAT from a VAT-inclusive amount or add it to a VAT-exclusive one? Formulas, a mixed-rate invoice, rounding gaps and shortcuts.
- How to Calculate Loan Payments: Formula, Step-by-Step Example and Interest vs. Principal Split – The monthly loan payment formula, a step-by-step example, the interest-versus-principal split and how to work back to the loan amount you can afford.
- How to Calculate Age From a Date of Birth: Age in Years, Months and Days – Work out your exact age in years, months and days, step by step. Month-end and leap-day edge cases, total days lived and common mistakes.
- How to Calculate BMI (Body Mass Index): Formula, Examples and Limits – How is BMI (body mass index) calculated from height and weight? Formula, examples, a weight-range table by height and its limits. General information only.
This article is for general information only and is not legal, financial or medical advice. We are not an official institution.