How to ignore a folder in gitignore

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, you need to add its path to your `.gitignore` file. Each line in the `.gitignore` file specifies a pattern, and Git will track all files and directories that match these patterns. For a folder, you simply list its name (relative to the `.gitignore` file's location) on a new line.

Key Facts

What is `.gitignore`?

The `.gitignore` file is a crucial component of Git version control that allows you to specify intentionally untracked files that Git should ignore. This is incredibly useful for keeping your repository clean and focused on your project's source code, rather than temporary files, build artifacts, user-specific configurations, or sensitive information.

Why Ignore Folders?

There are numerous reasons why you might want to ignore an entire folder:

How to Ignore a Folder in `.gitignore`

Ignoring a folder is as simple as adding its path to your `.gitignore` file. Here's the process:

1. Locate or Create `.gitignore`

The `.gitignore` file should be placed in the root directory of your Git repository. If you don't have one already, you can create it using your terminal:

touch .gitignore

Or by creating a new file named `.gitignore` in your project's root folder using your file explorer or IDE.

2. Add the Folder Path

Open the `.gitignore` file in a text editor. To ignore an entire folder, simply add the name of the folder followed by a forward slash (`/`) on a new line. The trailing slash is important as it specifically tells Git to ignore a directory.

Example: To ignore a folder named `build` located in the root of your repository, add the following line:

build/

If the folder you want to ignore is not in the root directory, you need to specify its path relative to the location of the `.gitignore` file.

Example: If you have a folder structure like this:

my-project/├── src/│ └── main.js├── dist/│ └── bundle.js└── logs/└── app.log

And your `.gitignore` file is in `my-project/`. To ignore the `logs` folder, you would add:

logs/

If you wanted to ignore a folder that's nested deeper, for instance, a `temp` folder inside `src`:

src/temp/

3. Special Cases and Patterns

Ignoring a folder and its contents, but not a file with the same name:

If you have a file named `logs` and a folder named `logs`, and you want to ignore the folder but not the file, using the trailing slash is sufficient:

logs/

Git will correctly interpret this as ignoring the directory named `logs`.

Ignoring all folders named `logs` anywhere in the repository:

You can use a double asterisk (`**`) for glob patterns. To ignore any folder named `logs` regardless of its depth:

** /logs/

Ignoring files within an ignored folder:

Files within a folder that is ignored by `.gitignore` are automatically ignored. You do not need to add them individually.

Ignoring a folder but keeping specific files within it:

This is a more advanced scenario. You can ignore a folder and then use negation (`!`) to re-include specific files or subdirectories. For example, to ignore the `build/` folder but keep the `build/index.html` file:

build/!build/index.html

Note that if you ignore a parent directory, you must explicitly re-include any subdirectories or files within it that you want Git to track. For example, to keep `build/assets/images/`:

build/!build/index.html!build/assets/!build/assets/images/

4. Commit the `.gitignore` File

After adding the folder path(s) to your `.gitignore` file, you need to add and commit the `.gitignore` file itself to your repository:

git add .gitignoregit commit -m "Add .gitignore file with ignored folder"

Important Considerations

By correctly configuring your `.gitignore` file, you ensure a cleaner, more manageable Git history and prevent unnecessary or sensitive files from being committed.

Sources

  1. Documentation/gitignore - Git SCMCC-BY-SA-4.0
  2. Ignoring files - GitHub Docsfair-use

Missing an answer?

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