.gitignore: Ignore Files and Folders in Git

When you work in a Git repository, you often create files that should stay on your machine: logs, dependency folders, build output, editor settings, and local environment files. Adding those paths to .gitignore keeps them out of commits and away from the remote repository.
The .gitignore file tells Git which untracked files and folders to ignore. This guide explains the most useful .gitignore patterns, including how to ignore a folder, how to stop tracking a committed file, and how to debug rules that do not behave as expected.
Quick Reference
For a printable quick reference, see the Git cheatsheet .
| Task | Pattern or command |
|---|---|
| Ignore a folder anywhere | folder_name/ |
| Ignore a folder in the repository root | /folder_name/ |
| Ignore all logs | *.log |
| Re-include a file | !important.log |
| Stop tracking a committed file | git rm --cached file |
| Stop tracking a committed folder | git rm -r --cached folder/ |
| Force-add an ignored file | git add -f file |
| Check why a path is ignored | git check-ignore -v path/to/file |
| Show ignored files | git status --ignored |
Ignore a Folder with .gitignore
To ignore a folder anywhere in the repository, add the folder name followed by a slash:
logs/The trailing slash tells Git to match only directories. This pattern ignores logs/, app/logs/, and any other directory with that name below the .gitignore file.
To ignore only a folder in the repository root, start the pattern with a slash:
/dist/This ignores dist/ at the root of the repository, but it does not ignore app/dist/.
If you need to keep an otherwise empty folder in the repository, ignore the folder contents and re-include a placeholder file:
logs/*
!logs/.gitkeepGit does not track empty directories, so .gitkeep is a common placeholder file. It is not a special Git feature.
If the folder was already committed before you added it to .gitignore, remove it from the Git index first:
git rm -r --cached logs/The --cached option keeps the folder in your working tree and removes it only from Git tracking.
What Files Should be Ignored?
Ignored files are usually platform-specific files or automatically created files from the build systems. Some common examples include:
- Runtime files such as log, lock, cache, or temporary files.
- Files with sensitive information, such as passwords or API keys.
- Compiled code, such as
.classor.o. - Dependency directories, such as
/vendoror/node_modules. - Build directories, such as
/public,/out, or/dist. - System files like
.DS_StoreorThumbs.db. - IDE or text editor configuration files.
.gitignore Patterns
.gitignore is a plain text file in which each line contains a pattern for files or directories to ignore.
It uses globbing patterns
to match filenames with wildcard characters. If you have files or directories containing a wildcard character, you can use a single backslash (\) to escape it.
Comments
Lines starting with a hash mark (#) are comments and are ignored. Empty lines can be used to improve the readability of the file and to group related lines of patterns.
Slash
The slash symbol (/) represents a directory separator. The slash at the beginning of a pattern is relative to the directory where the .gitignore resides.
If the pattern starts with a slash, it matches files and directories only in the repository root.
If the pattern does not start with a slash, it matches files and directories in any directory or subdirectory.
If the pattern ends with a slash, it matches only directories. When a directory is ignored, all of its files and subdirectories are also ignored.
Literal File Names
The most straightforward pattern is a literal file name without any special characters.
| Pattern | Example matches |
|---|---|
/access.log | access.log |
access.log | access.loglogs/access.logvar/logs/access.log |
build/ | build |
Wildcard Symbols
* - The asterisk symbol matches zero or more characters.
| Pattern | Example matches |
|---|---|
*.log | error.loglogs/debug.logbuild/logs/error.log |
** - Two adjacent asterisk symbols match any file or zero or more directories. When followed by a slash (/), it matches only directories.
| Pattern | Example matches |
|---|---|
logs/** | Matches anything inside the logs directory. |
**/build | var/buildpub/buildbuild |
foo/**/bar | foo/barfoo/a/barfoo/a/b/c/bar |
? - The question mark matches any single character.
| Pattern | Example matches |
|---|---|
access?.log | access0.logaccess1.logaccessA.log |
foo?? | fooabfoo23foo0s |
Square Brackets
[...] - Matches any of the characters enclosed in the square brackets. When two characters are separated by a hyphen - it denotes a range of characters. The range includes all characters that are between those two characters. The ranges can be alphabetic or numeric.
If the first character following the [ is an exclamation mark (!), then the pattern matches any character except those from the specified set.
| Pattern | Example matches |
|---|---|
*.[oa] | file.ofile.a |
*.[!oa] | file.sfile.1file.0 |
access.[0-2].log | access.0.logaccess.1.logaccess.2.log |
file.[a-c].out | file.a.outfile.b.outfile.c.out |
file.[a-cx-z].out | file.a.outfile.b.outfile.c.outfile.x.outfile.y.outfile.z.out |
access.[!0-2].log | access.3.logaccess.4.logaccess.Q.log |
Negating Patterns
A pattern that starts with an exclamation mark (!) negates (re-include) any file that is ignored by the previous pattern. The exception to this rule is to re-include a file if its parent directory is excluded.
| Pattern | Example matches |
|---|---|
*.log!error.log | error.log or logs/error.log will not be ignored |
.gitignore Example
Below is an example of what your .gitignore file could look like:
# Ignore the node_modules directory
node_modules/
# Ignore Logs
logs
*.log
# Ignore the build directory
/dist
# The file containing environment variables
.env
# Ignore IDE specific files
.idea/
.vscode/
*.sw*Local .gitignore
A local .gitignore file is usually placed in the repository’s root directory. However, you can create multiple .gitignore files in different subdirectories in your repository. The patterns in the .gitignore files are matched relative to the directory where the file resides.
Patterns defined in the files that reside in lower-level directories (sub-directories) have precedence over those in higher-level directories. For example, a logs/.gitignore containing !debug.log will re-include debug.log even if the root .gitignore ignores *.log.
Local .gitignore files are shared with other developers and should contain patterns that are useful for all other users of the repository.
Personal Ignore Rules
Patterns that are specific to your local repository and should not be distributed to other repositories should be set in the .git/info/exclude file.
For example, you can use this file to ignore generated files from your personal project tools.
Global .gitignore
Git also allows you to create a global .gitignore file, where you can define ignore rules for every Git repository on your local system.
Since Git 2.32+, Git automatically reads ~/.config/git/ignore if it exists, with no extra configuration needed. This is the recommended location on modern systems:
mkdir -p ~/.config/git
touch ~/.config/git/ignoreAlternatively, you can set a custom path. For example, to use ~/.gitignore_global as the global Git ignore file:
- Terminal
touch ~/.gitignore_global Add the file to the Git configuration:
Terminalgit config --global core.excludesfile ~/.gitignore_globalOpen the file with your text editor and add your rules to it.
Global rules are particularly useful for ignoring files that you never want to commit, such as files with sensitive information or compiled executables.
Ignoring Previously Committed Files
The files in your working copy can be either tracked or untracked.
To ignore a file that has been previously committed, you need to unstage and remove the file from the index, and then add a rule for the file in .gitignore:
git rm --cached filenameThe --cached option tells Git not to delete the file from the working tree but only to remove it from the index.
To recursively remove a directory, use the -r option:
git rm -r --cached directoryIf you want to remove the file from both the index and local filesystem, omit the --cached option.
When recursively deleting files, use the -n option to perform a “dry run” and show you what files will be deleted:
git rm -r -n directoryTo force-add a file that is currently ignored by .gitignore, use the -f flag:
git add -f ignored-file.logIf you need to remove untracked files from your working tree, see our guide on how to remove untracked files in Git .
Debugging .gitignore File
Sometimes it can be challenging to determine why a specific file is being ignored, especially when you are using multiple .gitignore files or complex patterns. This is where the git check-ignore
command with the -v option, which tells Git to display details about the matching pattern, comes in handy.
For example, to check why the www/yarn.lock file is ignored you would run:
git check-ignore -v www/yarn.lockThe output shows the path to the gitignore file, the number of the matching line, and the actual pattern.
www/.gitignore:31:/yarn.lock www/yarn.lockThe command also accepts more than one filename as arguments, and the file does not have to exist in your working tree.
Displaying All Ignored Files
The git status command with the --ignored option displays a list of all ignored files:
git status --ignoredFAQ
Why is my file still tracked after adding it to .gitignore?
Git only ignores untracked files. If a file is already tracked, remove it from the index with git rm --cached file.
Where should I put project-specific rules?
Use a local .gitignore in the repository so everyone on the team shares the same ignore rules.
How do I ignore files only on my machine?
Add patterns to .git/info/exclude or configure a global ignore file with core.excludesfile.
How can I see which rule is matching?
Run git check-ignore -v path/to/file to display the matching pattern and file.
Conclusion
The .gitignore file allows you to exclude files from being checked into the repository. The file contains globbing patterns that describe which files and directories should be ignored.
To get started quickly, GitHub maintains a collection of useful .gitignore templates at github/gitignore
. You can also use gitignore.io
to generate .gitignore files for your operating system, programming language, or IDE.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page