Ignoring Unwanted Files and Secrets using .gitignore Rules0%

Ignoring Unwanted Files and Secrets using .gitignore Rules

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Ignoring Unwanted Files and Secrets using .gitignore Rules

Not all files inside your project directory belong in version control. Build outputs, temporary OS files, package dependencies, and sensitive credentials must never be committed. Git uses the .gitignore configuration file to ignore these files automatically.


1. What Files Should Be Ignored?

In a professional software project, the following categories must be ignored:

  1. 1
    Secrets & Credentials: .env, secrets.json, .pem SSH keys, AWS credentials. (Committing these leads to critical security breaches!)
  2. 2
    Dependencies: node_modules/, venv/, vendor/, target/. (These can be regenerated via package managers like npm install or pip install).
  3. 3
    Build Artifacts & Compiled Code: dist/, build/, .next/, *.pyc, *.exe, *.dll.
  4. 4
    Operating System & Editor Files: .DS_Store (macOS), Thumbs.db (Windows), .idea/, .vscode/settings.json.
  5. 5
    Log Files & Temporary Dumps: *.log, npm-debug.log*, *.tmp.

2. Anatomy of a .gitignore File

A .gitignore file is a plain text file placed at the root of your repository. Each line specifies a pattern:

GITIGNORE
# 1. Ignore specific files
.env
secrets.json
 
# 2. Ignore all files ending with a specific extension
*.log
*.tmp
*.pyc
 
# 3. Ignore an entire directory (must end with trailing slash)
node_modules/
dist/
venv/
.next/
 
# 4. Ignore files in any subdirectory matching a pattern
**/temp/*.json
 
# 5. Whitelist exception using exclamation mark (!)
# Ignore all .log files EXCEPT important.log
*.log
!important.log

Pattern Matching Rules Table:

Pattern SyntaxMatchesExample
filename.extMatches filename.ext in any directory.env ignores all .env files
/filename.extMatches only in the root directory where .gitignore lives/TODO.txt ignores root TODO.txt only
directory/Matches the directory and all contents recursivelybuild/ ignores build folder
*Matches zero or more arbitrary characters*.pdf ignores all PDF files
**Matches directories across leading or nested paths**/logs/*.txt
!Negation rule (do not ignore this file)!release.log

3. The Golden Trap: Untracking Already-Tracked Files

Important Warning
[!CAUTION] If a file was already committed to Git in the past, adding it to .gitignore will NOT stop Git from tracking it! .gitignore only applies to untracked files.

How to Untrack an Already-Committed File:

To remove a file from Git's tracking without deleting the file from your local disk, use git rm --cached:

Bash / Terminal
# 1. Remove file from Git tracking while preserving it on disk
git rm --cached .env
 
# Or remove an entire folder (e.g., accidentally committed node_modules):
git rm -r --cached node_modules/
 
# 2. Commit the removal
git commit -m "chore: stop tracking .env and node_modules"

4. Global .gitignore for Developer-Specific Files

Operating system junk files like macOS .DS_Store or Windows Thumbs.db shouldn't clutter every team project's repo .gitignore. You can configure a personal global gitignore:

Bash / Terminal
# Set global excludes file
git config --global core.excludesFile ~/.gitignore_global
 
# Add OS patterns to that file
echo ".DS_Store" >> ~/.gitignore_global
echo "Thumbs.db" >> ~/.gitignore_global

Interactive Quiz

Practice Quiz

Question 1
What is the purpose of the .gitignore file in a Git project?
Question 2
Why should files containing API keys and database credentials (such as .env) NEVER be committed to Git?
Question 3
What does the pattern *.log match inside a .gitignore file?
Question 4
If you add secrets.json to .gitignore, but it was already committed last week, what happens?
Question 5
How do you untrack a file from Git without deleting the file from your local hard disk?
Next Lesson

Git Branching Mechanics: Pointers, HEAD, and git branch

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext