← Back to Git Course | Chapter 2: Basic Commands | Lesson 18 of 18

Git ignore (.gitignore)

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

bash
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

bash
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

bash
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

bash
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

bash
git check-ignore -v debug.log

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.