MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Ignoring Unwanted Files and Secrets using .gitignore Rules0%
Study Materials
Git & GitHub BasicsProgress 0%
1.1What is Version Control? (Local, Centralized vs Distributed VCS)1.2Why Git? History, Architecture & Core Benefits1.3The Three Trees: Working Directory, Staging Area & Git Repository1.4Installing Git and Initial Configuration (git config user.name / email)
3.1Inspecting Commit History with git log and Formatting Flags3.2Comparing Changes with git diff (Working Tree vs Staged vs HEAD)3.3Ignoring Unwanted Files and Secrets using .gitignore Rules
HomeTutorialsGit & GitHub BasicsIgnoring Unwanted Files and Secrets using .gitignore Rules
Git Branching Mechanics: Pointers, HEAD, and git branch

Ignoring Unwanted Files and Secrets using .gitignore Rules

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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:

1# 1. Ignore specific files
2.env
3secrets.json
4 
5# 2. Ignore all files ending with a specific extension
6*.log
7*.tmp
8*.pyc
9 
10# 3. Ignore an entire directory (must end with trailing slash)
11node_modules/
12dist/
13venv/
14.next/
15 
16# 4. Ignore files in any subdirectory matching a pattern
17**/temp/*.json
18 
19# 5. Whitelist exception using exclamation mark (!)
20# Ignore all .log files EXCEPT important.log
21*.log
22!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:

1# 1. Remove file from Git tracking while preserving it on disk
2git rm --cached .env
3 
4# Or remove an entire folder (e.g., accidentally committed node_modules):
5git rm -r --cached node_modules/
6 
7# 2. Commit the removal
8git 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:

1# Set global excludes file
2git config --global core.excludesFile ~/.gitignore_global
3 
4# Add OS patterns to that file
5echo ".DS_Store" >> ~/.gitignore_global
6echo "Thumbs.db" >> ~/.gitignore_global

Interactive Quiz• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Comparing Changes with git diff (Working Tree vs Staged vs HEAD)Git Branching Mechanics: Pointers, HEAD, and git branch

Practice Quiz

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

Mentor-Led Practical Training

Master Git & GitHub Basics with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext