.gitignore — الملف الذي يحميك من تضمين ملف node_modules

نُشرت في

دليل عملي لـ .gitignore: ما يفعله، كيفية عمل القواعد، أنماط شائعة لملفات node_modules وملفات .env، ومشكلة التتبع المسبق التي تؤثر على كل مطور.

.gitignore — The File That Saves You From Committing node_modules 1
إعلان · حذف؟

Every developer has done it at least once: accidentally committed node_modules, a .env file full of API keys, or a 200MB build artifact. The panic is real. The fix is tedious. And the whole thing was completely avoidable with a single file: .gitignore.

This guide explains what .gitignore is, how it works, what to put in it, and a few traps that catch even experienced developers.

What Is .gitignore?

.gitignore is a plain text file that tells Git which files and directories to exclude from version control. When Git sees a path listed in .gitignore, it pretends that path does not exist — it will not stage it, commit it, or show it in git status output.

The file lives at the root of your repository, though you can also place project-specific .gitignore files in subdirectories. Rules in a subdirectory file only apply to that directory and its children.

Why You Need One (The Short Version)

  • حماية — keeps secrets, API keys, and passwords out of your git history
  • Repo size — prevents generated files and dependencies from bloating your repo
  • Noise reduction — stops editor config and OS junk files from cluttering every diff
  • Team sanity — everyone clones a clean repo and installs dependencies locally

How the Syntax Works

The rules are simple but have a few non-obvious edges:

  • Blank lines and lines starting with # are ignored (use # for comments)
  • A pattern without a slash matches any file or directory with that name anywhere in the repo: *.log ignores every log file at every depth
  • A trailing slash matches directories only: dist/ ignores the directory but not a file named dist
  • A leading slash anchors the pattern to the repo root: /todo.txt only ignores a todo.txt at the very root
  • Double asterisk (**) matches across directory boundaries: **/logs coinciden logs/ anywhere in the tree
  • An exclamation mark (!) negates a pattern, re-including a file that a previous rule excluded

A Minimal Example

# Dependencies
node_modules/

# Environment files
.env
.env.local
.env.*.local

# Build output
dist/
build/

# Editor noise
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
npm-debug.log*

The Most Common Entries (and Why They Matter)

node_modules/

This is the big one. A typical Node.js project has thousands of files in node_modules/ — often hundreds of megabytes. None of it belongs in version control. Anyone who clones your repo runs npm install and rebuilds it locally from package.json. Always ignore it.

.env and secret files

Environment files hold database passwords, API keys, and service tokens. A committed .env is a security incident waiting to happen — GitHub scans for leaked secrets, but so do bots. Ignore the file, commit a .env.example with placeholder values instead so teammates know what variables to set.

dist/ and build/

Compiled or bundled output is derived from source. Your CI pipeline rebuilds it on every deploy. Committing build artifacts creates merge conflicts and false diffs that obscure real code changes.

Editor and OS files

.DS_Store (macOS), Thumbs.db (Windows), .idea/ (JetBrains), .vscode/ (VS Code settings) — these are personal workspace files. Committing them forces your preferences on every other contributor. Use a global ~/.gitignore_global for machine-specific noise so you do not have to add it to every project.

Global gitignore: Set It Once, Forget It

You can configure a global ignore file that applies to every repo on your machine:

git config --global core.excludesfile ~/.gitignore_global

Put editor files, OS junk, and personal tooling there. Reserve the project .gitignore for things the whole team agrees to ignore — like node_modules/ أو dist/.

The “Already Tracked” Trap

This is where developers get burned: .gitignore only prevents untracked files from being added. If a file is already in your git history, adding it to .gitignore does nothing. Git still tracks it and will still commit changes to it.

To stop tracking a file that is already committed:

# Remove the file from git tracking without deleting it locally
git rm --cached path/to/file

# Or remove a whole directory
git rm -r --cached node_modules/

# Then commit the removal
git commit -m "Stop tracking node_modules"

After that, the file stays on disk but Git ignores it going forward.

Negation Rules: Re-including Files

Sometimes you want to ignore a directory except for one specific file — for example, ignore config/ but keep config/defaults.json checked in:

config/
!config/defaults.json

One catch: negation cannot un-ignore a file inside an ignored directory. Git stops descending into ignored directories, so the ! rule never gets a chance to fire. You have to ignore the contents instead of the directory itself:

# Wrong — Git never sees defaults.json inside an ignored directory
config/
!config/defaults.json

# Right — ignore everything in config/ except defaults.json
config/*
!config/defaults.json

Generating a .gitignore for Your Stack

You do not have to write yours from scratch. gitignore.io (also at gitignore.io) lets you pick your language, framework, and editor and generates a comprehensive ignore file instantly. GitHub also offers official templates in its github/gitignore repository — these are well-maintained and cover hundreds of environments.

For most web projects, a solid starting point combines templates for your language (Node, Python, PHP, etc.), your editor (VS Code, JetBrains), and your OS (macOS, Windows).

Checking What Git Is Ignoring

Two commands help debug an unexpected ignore rule:

# See which files are being ignored in the current directory
git status --ignored

# Find out exactly which rule is causing a file to be ignored
git check-ignore -v path/to/file

git check-ignore -v is especially useful when a rule is not doing what you expect — it prints the file, line number, and pattern responsible.

Quick Reference: Pattern Cheat Sheet

نمطWhat It Ignores
*.logالكل .log files anywhere
/debug.logOnly debug.log at the root
logs/Any directory named logs
**/logslogs directory at any depth
!important.logRe-include important.log even if *.log matches it
doc/*.txt.txt files directly inside doc/ (not subdirs)
doc/**/*.txt.txt files anywhere under doc/
هل تريد حذف الإعلانات؟ تخلص من الإعلانات اليوم

تثبيت ملحقاتنا

أضف أدوات IO إلى متصفحك المفضل للوصول الفوري والبحث بشكل أسرع

أضف لـ إضافة كروم أضف لـ امتداد الحافة أضف لـ إضافة فايرفوكس أضف لـ ملحق الأوبرا

وصلت لوحة النتائج!

لوحة النتائج هي طريقة ممتعة لتتبع ألعابك، يتم تخزين جميع البيانات في متصفحك. المزيد من الميزات قريبا!

إعلان · حذف؟
إعلان · حذف؟
إعلان · حذف؟

ركن الأخبار مع أبرز التقنيات

شارك

ساعدنا على الاستمرار في تقديم أدوات مجانية قيمة

اشتري لي قهوة
إعلان · حذف؟