How to Use .gitignore in Git: Patterns, Examples and Common Fixes

A .gitignore file tells Git which untracked files and directories to leave out of normal staging and status results. It is commonly used for dependency folders, build output, editor files, logs and local environment settings. A good ignore file keeps a repository focused on the source files and configuration that teammates actually need.
This guide explains how to create a .gitignore file, understand pattern syntax, choose between project-level and personal ignore rules, and fix the common problem of a file that keeps appearing even after you add it to the ignore list. It also explains an important security limit: .gitignore does not remove files that Git already tracks and does not erase secrets from commit history.
Quick start: create a .gitignore file
Open a terminal in the root of your Git repository and create the file:
touch .gitignore
On Windows, you can also create a new text file named .gitignore in File Explorer or your code editor. Make sure the final filename is exactly .gitignore, not .gitignore.txt.
For a small web project, this starter example covers several common generated and local-only files. Keep only the rules that match your project; do not ignore a folder if your team intentionally commits its contents.
# Dependencies
node_modules/
# Build output at the repository root
/dist/
/build/
# Local environment files
.env
.env.*
!.env.example
# Logs and temporary files
*.log
*.tmp
# Python cache and virtual environment
__pycache__/
*.py[cod]
.venv/
# Test coverage output
coverage/
# Operating-system files
.DS_Store
Thumbs.db
Save the file, then review what Git sees:
git status --short
Once the rules are correct, add and commit the ignore file so the same rules are shared with anyone who clones the repository:
git add .gitignore
git commit -m "Add Git ignore rules"
The commit command assumes your repository is already initialized and your Git identity is configured. If you are new to GitHub authentication, see our guide to setting up SSH keys for GitHub.
What should you put in .gitignore?
Ignore files that are generated locally, can be recreated, vary from one developer’s machine to another, or contain private configuration. Typical examples include:
- Dependencies: directories such as
node_modules/when dependencies are installed from a package manifest and lockfile. - Build output: generated folders such as
dist/orbuild/, if your project does not intentionally version the built files. - Local configuration: environment-specific files such as
.envand local overrides. - Temporary output: logs, caches, coverage reports and temporary files.
- Personal editor and operating-system files: files created by a particular editor or desktop operating system.
Do not automatically ignore every folder named dist, build or vendor. Some projects deliberately commit compiled assets or dependencies for deployment. Decide based on the project’s build and release process.
How .gitignore patterns work
Each non-comment line in a .gitignore file is a pattern. The path is interpreted relative to the directory containing that ignore file. These examples show the differences that matter most.
| Pattern | What it matches |
|---|---|
*.log |
Files ending in .log at any level below that ignore file. |
/build/ |
A directory named build at the same level as the ignore file, not a nested directory with that name. |
build/ |
Directories named build anywhere below that ignore file. |
logs/debug.log |
The specified path relative to the directory containing the ignore file. |
**/cache/ |
A directory named cache at any depth. |
!important.log |
Re-includes a matching file that an earlier pattern excluded, subject to the parent-directory limitation. |
Wildcards: * and ?
An asterisk (*) matches any number of characters except a directory separator. A question mark (?) matches one character except a directory separator. For example:
# Ignore all .log files
*.log
# Ignore files such as debug1.tmp and debugA.tmp
debug?.tmp
Use ** when you need a pattern that crosses directory levels. For example, **/cache/ matches a directory named cache anywhere below the ignore file.
Leading and trailing slashes
A slash at the beginning anchors a pattern to the directory containing the .gitignore file. A slash at the end means the pattern matches directories only. For example, /build/ ignores the root-level build directory, while build/ can match a directory with that name in a nested location too.
If a pattern contains a slash in the middle, such as logs/debug.log, it is already relative to the location of that .gitignore file; adding a leading slash is not required for that purpose.
Comments and exceptions with !
Use a line beginning with # for a comment. Use ! at the beginning of a pattern to reverse an earlier ignore rule. This is useful when you want to ignore most environment files but keep a safe example file:
.env
.env.*
!.env.example
The negation rule has a limitation: Git cannot re-include a file if a parent directory of that file is excluded. If you need to keep one file inside a directory, avoid excluding the parent directory wholesale; write narrower patterns for the files you actually want to ignore.
Where should ignore rules live?
Git has several places for ignore rules. Choose the location based on whether the rule should be shared.
1. Repository .gitignore
Use a .gitignore file in the repository when all contributors should use the same rules. Commit it so that it is included when teammates clone the project. You can also add more specific .gitignore files inside subdirectories; their patterns are relative to those directories.
2. .git/info/exclude for one local repository
If a file is specific to your own workflow and other contributors do not need the rule, add it to .git/info/exclude inside that repository. This file is local to your clone and is not normally committed.
3. A global ignore file for your computer
Use a global ignore file for personal files you want Git to ignore in every repository, such as operating-system metadata or editor swap files. Git reads the path configured by core.excludesFile. You can set it to a file in your home directory, for example:
git config --global core.excludesFile ~/.config/git/ignore
Create the parent directory and the ignore file if they do not already exist. The exact path can be different on your computer. Keep project-wide rules in the repository’s .gitignore; keep personal machine rules in the global file.
Why is Git still tracking a file in .gitignore?
This is the most common misunderstanding. Ignore rules are designed to keep untracked files untracked. They do not automatically stop tracking a file that was already added to the repository or committed.
If a file is already tracked and you want to keep it on your computer but remove it from the next version of the repository, first add the correct rule to .gitignore, then remove the path from Git’s index:
git rm --cached .env
git status
The --cached option removes the file from Git’s index but leaves your working copy on disk. Review the staged change, then commit it:
git commit -m "Stop tracking local environment file"
For a tracked directory, use the recursive option and check the path carefully before committing:
git rm -r --cached path/to/generated-folder
This changes what future commits track; it does not erase earlier commits. If a password, API key or token has already been pushed to a remote repository, treat it as exposed: revoke or rotate it first. Removing the file in a later commit is not enough to make the old secret safe. Rewriting repository history is a separate operation that needs careful coordination with collaborators.
How to check which rule is ignoring a file
When a file is unexpectedly ignored—or is not ignored when you expect it to be—use Git’s diagnostic command:
git check-ignore -v path/to/file
The verbose output identifies the ignore source, line number, matching pattern and path. This helps you find whether the rule came from a repository .gitignore, a nested ignore file, the local exclude file or your global ignore file.
By default, git check-ignore does not report tracked files because ignore rules do not apply to them. To inspect a tracked path while debugging a rule, add --no-index:
git check-ignore -v --no-index path/to/file
If the file is not ignored, the command may produce no matching output. Check the spelling, directory level, slash placement and whether another pattern later in the same precedence level changes the result.
Common .gitignore mistakes to avoid
- Expecting it to untrack an existing file: use
git rm --cachedand commit the index change. - Ignoring a parent directory and trying to re-include a child: Git cannot re-include a file while its parent directory remains excluded. Narrow the parent rule.
- Using a root-only pattern unintentionally:
/build/andbuild/have different scopes. - Ignoring files the project actually needs: check whether generated assets or dependencies are expected to be committed before excluding them.
- Assuming .gitignore protects secrets: it is a convenience for untracked files, not a security boundary. Review staged changes before committing.
- Putting personal editor rules in the shared file: use the global ignore file or
.git/info/excludewhen other contributors should not inherit the rule.
Frequently asked questions
Does .gitignore delete files from my computer?
No. It tells Git to ignore matching untracked paths. It does not delete the files from your working directory.
Can I have more than one .gitignore file?
Yes. A repository can have a root .gitignore and additional files in subdirectories. Their patterns apply relative to the location of each ignore file.
Should I commit the .gitignore file?
Usually yes. Commit the repository’s .gitignore so the same project rules apply to everyone who clones the repository. Keep personal-only rules in .git/info/exclude or a global ignore file instead.
Can .gitignore prevent an API key from being exposed?
It can help prevent an untracked environment file from being added accidentally, but it cannot protect a secret that is already tracked or force-added. Store secrets outside source control, review staged files, and rotate any credential that has been exposed.
Where can I find a starter .gitignore for my language or editor?
GitHub maintains a collection of example templates for common languages, frameworks, operating systems and editors in its official github/gitignore repository. Review a template before using it and remove rules that do not fit your project.
Conclusion
A well-maintained .gitignore keeps generated files, local settings and temporary output out of routine Git changes. Start with a small set of rules, understand how slashes and wildcards affect matching, and use git check-ignore -v when a rule behaves unexpectedly. Most importantly, remember that ignore rules affect untracked files only: remove already-tracked paths from the index separately, and rotate any secret that has reached a remote repository.
Official references
- Git documentation: gitignore pattern format and precedence
- Git documentation: git check-ignore
- Git documentation: git rm –cached
- GitHub Docs: Ignoring files
- GitHub: Official .gitignore templates
- GitHub Docs: Remediating a leaked secret
- GitHub Docs: Removing sensitive data from a repository





