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
Key Facts
- The `.gitignore` file should be placed in the root directory of your Git repository.
- Each line in `.gitignore` specifies a pattern for files or directories to ignore.
- Wildcards like `*` can be used for more flexible pattern matching.
- Once a file is already tracked by Git, adding it to `.gitignore` won't untrack it; you must first remove it from tracking.
- `.gitignore` files can be nested in subdirectories for more granular control.
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:
- 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. - 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.jsUnderstanding `.gitignore` Patterns
The patterns in `.gitignore` follow specific rules:
- Blank lines are ignored.
- Comments can be added using the hash symbol (`#`) at the beginning of a line.
- Trailing spaces are usually ignored unless they are quoted.
- Standard glob patterns are supported.
- A leading directory separator (`/`) matches only directories. For example, `build/` will ignore a directory named `build`, but `build` would ignore files or directories named `build`.
- A trailing slash (`/`) indicates that the pattern should only match directories.
- An exclamation mark (`!`) at the beginning of a pattern negates the pattern; files or directories matching the negated pattern will be included, even if they match a previous pattern. This is useful for ignoring a whole directory but including specific files within it.
- `**` matches zero or more directories. For example, `**/logs` would match any directory named `logs` anywhere in the repository.
Common Examples:
*.log: Ignore all files ending with `.log`.node_modules/: Ignore the `node_modules` directory and its contents.build/: Ignore the `build` directory..env: Ignore a specific file named `.env`.temp/*: Ignore all files and directories directly within the `temp` directory.- `!important.log`: This line, if placed after `*.log`, would ensure that `important.log` is not ignored.
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:
- Create a global ignore file somewhere on your system, for example, `~/.gitignore_global`.
- 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
- Commit `.gitignore`: Always commit your `.gitignore` file to your repository. This ensures that everyone working on the project adheres to the same ignore rules.
- Be specific: Use specific patterns rather than overly broad ones to avoid accidentally ignoring files you actually need.
- Use comments: Add comments to your `.gitignore` file to explain why certain files or directories are being ignored.
- Check before committing: Regularly use
git statusto see which files are being tracked and which are ignored.
By effectively using `.gitignore`, you can maintain a cleaner repository, avoid committing unnecessary files, and streamline your Git workflow.
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.