TR

Home / Guides / What Is Gitignore? How to Write a .gitignore File, With Example Templates

What Is Gitignore? How to Write a .gitignore File, With Example Templates

Updated: October 13, 2026 · AracMerkezi

▶ Open .gitignore Generator

.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

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

  1. Saving the file as .gitignore.txt.
  2. Assuming that a tracked file will be ignored.
  3. Leaving unnecessary trailing spaces at the end of a pattern.
  4. Ignoring lock files (such as package-lock.json); these are usually committed to the repository.
  5. 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

  1. Before starting the project, create the .gitignore file with suitable templates.
  2. Use git status to check that unwanted files do not appear in the list.
  3. Add example files that contain no real values, such as .env.example.
  4. 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

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

This article is for general information only and is not legal, financial or medical advice. We are not an official institution.