Ignoring Files with .gitignore
Not everything belongs in version control — build outputs, dependencies, secrets, and editor junk should stay out. By the end of this lesson you'll write .gitignore patterns confidently, use negation, untrack files you already committed, and set up a global ignore.
Learn Ignoring Files with .gitignore in our free Git course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Git course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1️⃣ Writing .gitignore
A .gitignore file lives at your repo root. Each line is a pattern : a bare name ignores one file, *.log ignores every file with that extension, and a trailing slash like build/ ignores a whole directory. Lines starting with # are comments. Commit the .gitignore so your whole team shares the rules.
2️⃣ Patterns, Globs & Negation
Patterns give you fine control. A leading slash anchors to the root, ** matches across any number of directories , and a leading ! negates an earlier rule to re-include a file. Test any path with git check-ignore -v <path> — it prints the matching rule, or nothing (exit code 1) if the path isn't ignored.
3️⃣ Untracking an Already-Committed File
The single biggest gotcha: .gitignore only affects untracked files. If a file is already tracked, adding it to .gitignore does nothing — Git keeps committing it. To stop, untrack it with git rm --cached , which removes it from Git's index but keeps it on disk . Then add it to .gitignore and commit.
4️⃣ A Global gitignore
Some clutter — editor swap files, .DS_Store , IDE folders — appears in every project but shouldn't pollute each project's shared .gitignore . Set up a global ignore once with git config --global core.excludesfile , and it applies to all your repos.
Your turn. Ignore all logs but keep one, then untrack a file you committed by mistake. Fill in the three blanks.
📋 Quick Reference
No commands given this time — just the plan. Write a .gitignore that keeps only your real source files tracked.
Practice quiz
What is the purpose of a .gitignore file?
- To delete files
- To list your commits
- To tell Git which untracked files to ignore
- To store passwords safely
Answer: To tell Git which untracked files to ignore. .gitignore lists patterns of files Git should not track or show as untracked.
What does the pattern match?
- Every file ending in .log
- Only a file literally named *.log
- Files in a log/ folder
- Nothing
Answer: Every file ending in .log. The * wildcard matches any characters, so *.log matches all files with the .log extension.
What does a trailing slash mean, as in ?
- Ignore a file named build
- Ignore files starting with build
- It's invalid syntax
- Ignore only the build directory (and its contents)
Answer: Ignore only the build directory (and its contents). A trailing slash restricts the match to directories.
What does a leading do, as in ?
- Forces deletion
- Negates a previous ignore — re-includes the file
- Comments out the line
- Matches hidden files
Answer: Negates a previous ignore — re-includes the file. ! negates an earlier pattern, so a file otherwise ignored becomes tracked again.
A file is ALREADY tracked. Adding it to .gitignore...
- Has no effect until you run git rm --cached on it
- Immediately untracks it
- Deletes it
- Causes an error
Answer: Has no effect until you run git rm --cached on it. .gitignore only affects untracked files; you must git rm --cached an already-tracked file to stop tracking it.
What does do?
- Deletes the file from disk
- Adds it to the next commit
- Stops Git tracking the file but keeps it on disk
- Ignores all .env files
Answer: Stops Git tracking the file but keeps it on disk. --cached removes the file from the index (untracks it) while leaving the working-tree copy intact.
Lines starting with in .gitignore are...
- Negations
- Comments
- Directory patterns
- Errors
Answer: Comments. A # at the start of a line marks a comment in .gitignore.
Which sets up a GLOBAL gitignore for all your repos?
- git ignore --global
- echo to .gitignore-all
- git global-ignore
- git config --global core.excludesfile ~/.gitignore_global
Answer: git config --global core.excludesfile ~/.gitignore_global. Setting core.excludesfile points Git at a personal ignore file applied across all repositories.
What does mean in a pattern like ?
- Exactly one directory
- Any number of directories (including none)
- Only hidden directories
- A literal asterisk
Answer: Any number of directories (including none). ** matches across multiple directory levels.
Where should project-specific ignore rules normally live?
- In your home folder only
- In the commit message
- In a .gitignore file committed at the repo root
- In a branch name
Answer: In a .gitignore file committed at the repo root. A committed .gitignore at the repo root shares the rules with the whole team.