How to ignore a folder 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 folder in Git, create a file named `.gitignore` in the root of your repository and add the folder's name (or a path pattern) to this file. Git will then stop tracking changes within that folder and its contents.

Key Facts

Overview

In software development, it's common to have certain files or directories that you don't want to track with Git. This can include compiled code, dependency directories (like `node_modules`), log files, or temporary files. Git provides a mechanism to ignore these files and directories using a special file called `.gitignore`.

What is `.gitignore`?

The `.gitignore` file is a plain text file that tells Git which files or directories it should intentionally ignore. When Git encounters a file or directory that matches a pattern listed in `.gitignore`, it will not stage or commit changes to it. This keeps your repository clean and focused on the essential code and configuration files.

How to Create and Use `.gitignore`

Creating a `.gitignore` file is straightforward:

  1. Create the file: In the root directory of your Git repository (the same directory where your `.git` folder is located), create a new file named `.gitignore`. You can do this using your text editor or the command line: touch .gitignore.
  2. Add patterns: Open the `.gitignore` file in your text editor and add the names of the files or directories you want to ignore, one per line. For example, to ignore a folder named `logs` and a file named `config.local.js`, you would add:
logs/config.local.js

Understanding `.gitignore` Patterns

The patterns in `.gitignore` follow specific rules:

Common Examples:

Ignoring Already Tracked Files

If you've already committed a file or directory to your Git repository and then decide to ignore it by adding it to `.gitignore`, Git will continue to track its changes. To stop tracking an already committed file or directory, you need to explicitly remove it from Git's index without deleting it from your working directory. You can do this using the following command:

git rm --cached 

For example, to stop tracking the `logs/` directory:

git rm --cached -r logs/

After running this command, commit the changes:

git commit -m "Stop tracking logs/ directory"

Now, Git will ignore the `logs/` directory and its contents in future commits, provided 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., editor backup files, OS-specific files), you can configure a global `.gitignore` file. This is particularly useful for personal preferences that don't belong in project-specific `.gitignore` files.

To set up a global ignore file:

  1. Create a global ignore file somewhere on your system, for example, `~/.gitignore_global`.
  2. Configure Git to use this file:
    git config --global core.excludesfile ~/.gitignore_global

Any patterns added to `~/.gitignore_global` will be ignored by Git for all your repositories.

Best Practices

By effectively using `.gitignore`, you can maintain a cleaner repository, avoid committing unnecessary files, and streamline your Git workflow.

Sources

  1. Documentation/gitignore - git-scm.comCC-BY-SA-4.0
  2. GitHub's collection of gitignore templatesCC0-1.0

Missing an answer?

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