Skip to main content

.gitignore Cheatsheet

By Dejan Panovski •Updated on • Download PDF

.gitignore pattern syntax at a glance: wildcards, directory rules, negation, anchoring, global ignore files, untracking committed files, and git check-ignore.

A .gitignore file lists patterns for untracked files that Git should leave out of git status and git add. Each line holds one pattern. Git matches it relative to the directory that contains the file. The rules below were tested with Git 2.53 on Ubuntu 26.04.

Pattern Basics

A slash at the start or in the middle anchors the pattern to the directory of the .gitignore file.

PatternDescription
# commentComment; the # must be the first character on the line
debug.logFile or directory named debug.log at any depth
logs/Directory named logs at any depth, never a file
/TODOTODO next to the .gitignore file only
/dist/Root dist directory, not app/dist/
doc/frotzMatches doc/frotz, not a/doc/frotz
**/doc/frotzdoc/frotz at any depth
\#notesLiteral name #notes at any depth
\!draftLiteral name !draft at any depth
name\Keep a trailing space; unescaped trailing spaces are dropped

Wildcards

*, ?, and [...] never match a slash. Use ** to cross directories.

PatternDescription
*.logEvery .log file at any depth
access?.logOne character: access1.log, not access10.log
access.[0-2].logaccess.0.log, access.1.log, or access.2.log
*.[oa]Files that end in .o or .a
*.[!oa]One-character extension other than o or a
**/buildbuild at any depth, same as build
logs/**Everything inside the root logs directory
a/**/ba/b, a/x/b, a/x/y/b
src/*/temp/temp exactly one level below src
src/**/temp/temp at any depth below src, including src/temp
docs/**/*.pdfPDF files anywhere under docs

Negation

A pattern that starts with ! re-includes a path. The last matching line wins.

PatternDescription
!error.logAfter *.log, keep every error.log
!.env.exampleAfter .env*, keep the example file
!logs/.gitkeepAfter logs/*, keep a placeholder in the empty directory
!/src/After /*, keep only the root src directory
!*/After *, re-include directories so later ! rules can reach their files
!*.mdAfter * and !*/, keep only Markdown files
!/.gitignoreKeep the .gitignore file itself when /* or * ignores everything

Rule Files

A .gitignore rule wins over .git/info/exclude, which wins over the global file.

File or CommandDescription
.gitignoreShared rules; commit it with the project
app/.gitignoreRules relative to app/; they override rules from parent directories
.git/info/excludeRules for one clone only; never committed
~/.config/git/ignoreGlobal rules for every repository (default path)
$XDG_CONFIG_HOME/git/ignoreGlobal default path when XDG_CONFIG_HOME is set
git config --global core.excludesFile ~/.gitignore_globalUse a custom global ignore file
git config --get core.excludesFileShow the configured global ignore file

Untracking Files

Ignore rules apply only to untracked files. When others pull the commit, Git deletes the file from their working tree.

CommandDescription
git rm --cached fileStop tracking a file and keep it on disk
git rm -r --cached dir/Stop tracking a directory
git rm -r --cached -n dir/Dry run: list the files that would be untracked
git ls-files -ci --exclude-standardTracked files that match an ignore rule
git add -f fileAdd a file that an ignore rule matches

Checking Rules

git check-ignore reads the same rule files as git add.

CommandDescription
git check-ignore -v pathFile, line number, and pattern that match path
git check-ignore -v -n pathAlso print paths that match no rule (shown with ::)
git check-ignore -v --no-index pathInclude tracked files, which are skipped by default
git check-ignore -q pathExit status only: 0 ignored, 1 not ignored
git status --ignoredList ignored files and directories (!! in --short output)
git status --ignored --untracked-files=allList each file inside ignored directories
git ls-files --others --ignored --exclude-standardEvery ignored file, one per line

Cleaning Ignored Files

Run the -n preview first. git clean -f deletes files without a confirmation prompt.

CommandDescription
git clean -ndXPreview: only ignored files and directories
git clean -fdXDelete ignored files, such as build output
git clean -ndxPreview: ignored and untracked files
git clean -fdxDelete ignored and untracked files
git stash -aStash changes plus untracked and ignored files instead of deleting them

Common Patterns

Add only the lines that match your stack.

PatternDescription
node_modules/npm, pnpm, and Yarn dependencies
dist/Build output from JavaScript bundlers
target/Rust and Maven build output
*.oC and C++ object files
__pycache__/Python bytecode cache
.venv/Python virtual environment
vendor/PHP Composer dependencies
.env*Local environment files; add !.env.example to keep the template
*.logLog files
coverage/Test coverage reports
.idea/JetBrains IDE settings
.vscode/*VS Code settings; add !.vscode/extensions.json to share extension recommendations
*.swpVim swap files
*~Backup files from Emacs, gedit, and other editors
.DS_StoremacOS directory metadata
Thumbs.dbWindows thumbnail cache

Troubleshooting

Quick checks for rules that do not behave as expected.

IssueCheck
File still appears in git statusCheck git ls-files --error-unmatch -- file; if tracked, use git rm --cached file. Otherwise, check the rule with git check-ignore -v file
! rule has no effectThe parent directory is ignored; use logs/* instead of logs/
! rule above the pattern it should overrideMove it below; the last matching line wins
check-ignore -v prints a ! patternA negation matched, so the path is not ignored (the exit status is still 0)
check-ignore prints nothingThe path is tracked or matches no rule; add --no-index or -n
doc/frotz misses a/doc/frotzA middle slash anchors the pattern; use **/doc/frotz
*.log # logs matches nothingComments work only on their own line
Empty directory never shows upGit tracks files, not directories; add a placeholder such as .gitkeep
The following paths are ignored by one of your .gitignore filesgit add skipped an ignored path; use git add -f to add it anyway
Pattern works on macOS but not on LinuxGit normally sets core.ignoreCase=true when it detects a case-insensitive filesystem; match the file name case for portable rules

Use these guides for full Git workflows.

GuideDescription
.gitignore GuideIgnore files and directories, with examples
Git CheatsheetEveryday Git commands
Remove Untracked Filesgit clean options and dry runs
git stashSave uncommitted, untracked, and ignored files
Undo the Last CommitRemove a file you committed by mistake