Tour of Git
Git Basics / Lesson 1.8

.gitignore — Ignoring Files

Not everything in your project folder belongs in Git. Build artifacts, dependency folders, secret files, and OS junk should be kept out of your repository. The .gitignore file tells Git which files and directories to ignore.

How It Works

Create a file named .gitignore in the root of your repository. Each line is a pattern that Git will skip when tracking files:

node_modules/
.env
*.log
.DS_Store

Files matching these patterns will not appear in git status and will not be staged by git add ..

Common Patterns

PatternWhat it matches
node_modules/The entire node_modules directory
*.logAny file ending in .log
.envA specific file named .env
dist/A build output directory
*.tmpAll temporary files
.DS_StoremacOS Finder metadata
__pycache__/Python cache directory
*.oCompiled object files

Pattern Rules

  • Blank lines are ignored (use them for readability)
  • Lines starting with # are comments
  • A trailing / means "directory only"
  • A leading ! negates a pattern (re-includes a file)
  • * matches anything except /
  • ** matches across directories

Example with negation:

*.log
!important.log

This ignores all .log files except important.log.

Try it!

The repository is already initialized. Create some files that should be ignored:

  1. Create files to ignore: echo "secret" > .env and echo "debug info" > debug.log
  2. Check status: git status (both files appear as untracked)
  3. Create a .gitignore file: echo -e ".env\n*.log" > .gitignore
  4. Check status again: git status (only .gitignore appears now!)
  5. Stage and commit: git add .gitignore then git commit -m "Add gitignore file"
  6. Verify ignored files are still present on disk: ls -a

The files are still in your folder — Git just pretends they don't exist.

Important: Already-Tracked Files

If a file is already tracked by Git (previously committed), adding it to .gitignore won't remove it. You need to untrack it first:

git rm --cached secret.txt

Then commit, and from that point on .gitignore will apply.

🎯

Goal

Create a .gitignore file to ignore log files and verify they don't appear in status

Terminal
$

No commits yet. Run `git init` and create your first commit.