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

Git ignore (.gitignore)

A .gitignore file is like a do-not-photograph list for the album. Anything on the list, like secret passwords or messy temporary files, is never included.
Syntax
bash
# .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

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Adding a file to .gitignore after it is already committed and expecting it to disappear, when Git keeps tracking it until you run git rm --cached file.
  2. Writing *.log in the wrong place or with a typo such as *.logs, so the intended files are not ignored.
  3. Expecting .gitignore to protect secrets already pushed, when the earlier commits still contain them.
Chapter Summary
  • git init, git clone, git add, and git commit create repositories and record changes, using the staging environment.
  • git status, git log, git diff, and git show inspect what has changed.
  • git rm, git mv, git restore, git clean, git stash, tags, and .gitignore manage files and work in progress.

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.