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

Quick Answer: To ignore a file in Git, you create a `.gitignore` file in your repository's root directory. You then list the names or patterns of the files and directories you want Git to ignore within this file. This prevents them from being tracked or accidentally committed.

Key Facts

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:

Commonly Ignored Files

Here are some common examples of files and directories that are good candidates for `.gitignore`:

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.db

By effectively using `.gitignore`, you ensure that your Git repository remains clean, relevant, and secure, focusing only on the essential project files.

Sources

  1. Documentation/gitignore - Git SCMCC-BY-SA-4.0
  2. Ignoring files from your entire computer - GitHub Docsfair-use
  3. Ignoring Files with Gitignore | Atlassian Git Tutorialfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.