Git ignore (.gitignore)
In this page:
Ignoring Specific Files
A .gitignore file lists patterns Git should never track -- to ignore one specific file, just write its exact name (like secrets.env) on its own line, and Git will stop offering it up in git status or git add.
Example: Ignoring Specific Files
echo "secrets.env" >> .gitignore
Ignoring Files by Extension
The * wildcard matches any characters, so *.log ignores every file ending in .log regardless of name -- a common pattern for temporary build output or debug logs you never want committed by accident.
Example: Ignoring Files by Extension
echo "*.log" >> .gitignore
Ignoring Directories
Ending a pattern with a trailing slash, like node_modules/, tells Git to ignore an entire directory and everything inside it, which is essential for excluding large dependency folders that shouldn't live in version control.
Example: Ignoring Directories
echo "node_modules/" >> .gitignore
Creating Exceptions to Ignore Rules
Sometimes you want a broad ignore rule with a specific exception -- prefixing a pattern with !, like !important.log, tells Git to track that one file even though a wider rule would otherwise ignore it.
Example: Creating Exceptions to Ignore Rules
echo "*.log" >> .gitignore
echo "!important.log" >> .gitignore
Checking Ignored Status
If a file mysteriously refuses to be tracked and you can't tell why, git check-ignore -v <file> shows exactly which line, in which .gitignore file, is responsible for excluding it -- much faster than guessing.
Example: Checking Ignored Status
git check-ignore -v debug.log
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)