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
Key Facts
- The `.gitignore` file is a plain text file.
- Patterns in `.gitignore` are matched relative to the file's location.
- An empty line in `.gitignore` matches nothing.
- A line starting with `#` is a comment.
- To ignore a folder named 'logs' in the root directory, add `logs/` to `.gitignore`.
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:
- Build Output: Directories generated by build tools (e.g., `dist/`, `build/`, `bin/`) often contain compiled code or executables that don't need to be version controlled.
- Dependencies: Package manager dependency folders (e.g., `node_modules/` for Node.js, `vendor/` for PHP) are typically installed from external sources and are best left out of your repository.
- Logs and Temporary Files: Folders containing application logs (`logs/`), temporary files (`tmp/`), or cache directories should not be committed.
- IDE/Editor Specific Files: Many Integrated Development Environments (IDEs) and text editors create configuration or workspace files that are specific to your local setup (e.g., `.idea/`, `.vscode/`, `*.sublime-project`).
- Sensitive Information: Folders containing secrets, API keys, or credentials should never be committed to version control.
- Operating System Generated Files: Some operating systems create hidden files or directories that are not relevant to the project (e.g., `.DS_Store` on macOS).
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
- Already Tracked Files: If the folder or files within it have already been tracked by Git (i.e., committed previously), adding them to `.gitignore` will not stop Git from tracking them. You must first remove them from Git's tracking using:
git rm -r --cached your_folder_name/
Then, commit this removal and ensure your `.gitignore` file is updated.
git config --global core.excludesfile ~/.gitignore_global
And then create and manage the `~/.gitignore_global` file.
By correctly configuring your `.gitignore` file, you ensure a cleaner, more manageable Git history and prevent unnecessary or sensitive files from being committed.
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
- Documentation/gitignore - Git SCMCC-BY-SA-4.0
- Ignoring files - GitHub Docsfair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.