How to ignore a file in git
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- The `.gitignore` file is a plain text file.
- It should be placed in the root directory of your Git repository.
- Patterns in `.gitignore` can match file names, directory names, and use wildcards.
- You can have multiple `.gitignore` files in different subdirectories.
- Once a file is tracked, adding it to `.gitignore` won't untrack it; you need to use `git rm --cached <file>` first.
Overview
Git is a powerful version control system that helps developers track changes in their code. However, not all files in a project are meant to be version controlled. This includes temporary files, build artifacts, log files, sensitive configuration data, and dependency directories. Ignoring these files prevents your repository from becoming cluttered and avoids committing unnecessary or private information. The primary mechanism for telling Git which files to ignore is the `.gitignore` file.
What is `.gitignore`?
The `.gitignore` file is a special file that tells Git which files or directories it should intentionally skip. When Git encounters a file that matches a pattern in a `.gitignore` file, it will not stage, commit, or show it in the status output as an untracked file. This is crucial for maintaining a clean and focused repository.
Where to Place `.gitignore`
The most common and recommended place for a `.gitignore` file is in the root directory of your Git repository. This single file can then contain rules that apply to the entire project. However, you can also place `.gitignore` files in subdirectories. Rules in a subdirectory's `.gitignore` file apply to that directory and its subdirectories, in addition to the rules from parent `.gitignore` files.
Syntax and Patterns
The `.gitignore` file uses a simple pattern-matching syntax:
- Blank lines and lines starting with a # are ignored, serving as comments.
- Standard filenames (e.g., `log.txt`) will ignore that specific file.
- Leading slash (e.g., `/TODO`) ignores the file only in the root directory.
- Trailing slash (e.g., `build/`) ignores the directory named 'build' and everything within it.
- Wildcards can be used:
- `*` matches zero or more characters (e.g., `*.log` ignores all files ending in `.log`).
- `?` matches exactly one character (e.g., `file?.txt` matches `file1.txt` but not `file10.txt`).
- `[]` matches any character within the brackets (e.g., `[abc].txt` matches `a.txt`, `b.txt`, and `c.txt`).
- Negation: Prefixing a pattern with an exclamation mark (
!) negates it. This means files matching the negated pattern will be tracked even if they match another pattern that would ignore them. For example, `*.log` followed by `!important.log` will ignore all `.log` files except `important.log`.
Commonly Ignored Files
Here are some common examples of files and directories that are good candidates for `.gitignore`:
- Build output: Directories like `bin/`, `obj/`, `target/`, `dist/`.
- Dependency directories: `node_modules/`, `vendor/`.
- Log files: `*.log`.
- IDE/Editor specific files: `.idea/`, `.vscode/`, `*.swp`.
- OS specific files: `.DS_Store` (macOS), `Thumbs.db` (Windows).
- Temporary files: `*.tmp`.
- Secrets/Credentials: `config.local.json`, `.env`.
Ignoring Already Tracked Files
If you have already committed a file to your repository and later decide to ignore it, adding it to `.gitignore` won't be enough. Git will continue to track changes to files that are already tracked. To stop tracking a file without deleting it from your working directory, you need to use the following command:
git rm --cached
After running this command, Git will remove the file from its index (staging area) but leave it in your working directory. Now, when you commit, the file will be ignored because it's no longer tracked, and it's listed in your `.gitignore` file.
Global `.gitignore`
For files that you want to ignore across all your Git repositories on your machine (e.g., OS-specific files like `.DS_Store`), you can configure a global `.gitignore` file. You can specify the path to this file using:
git config --global core.excludesfile ~/.gitignore_global
Then, create the `~/.gitignore_global` file and add your patterns there.
Example `.gitignore` for a Node.js project
# Dependenciesnode_modules/# Logslogs*.lognpm-debug.log*yarn-debug.log*yarn-error.log*# Runtime datapids*.pid*.seed*.pid.lock# Build outputlib*.js.map# Optional npm cache directory.npm# Optional eslint cache.eslintcache# IDE settings.vscode/.idea/# OS generated files.DS_StoreThumbs.dbBy effectively using `.gitignore`, you ensure that your Git repository remains clean, relevant, and secure, focusing only on the essential project files.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
Missing an answer?
Suggest a question and we'll generate an answer for it.