Git ignore (.gitignore)
In this page:
# .gitignore
file_name
*.extension
directory_name/
!exception_file
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
- Adding a file to
.gitignoreafter it is already committed and expecting it to disappear, when Git keeps tracking it until you rungit rm --cached file. - Writing
*.login the wrong place or with a typo such as*.logs, so the intended files are not ignored. - Expecting
.gitignoreto protect secrets already pushed, when the earlier commits still contain them.
git init,git clone,git add, andgit commitcreate repositories and record changes, using the staging environment.git status,git log,git diff, andgit showinspect what has changed.git rm,git mv,git restore,git clean,git stash, tags, and.gitignoremanage files and work in progress.
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)