How to configure .gitignore and what to add?
The .gitignore file tells Git which files and directories to ignore — not track and not include in commits. This is critical for repository cleanliness.
What to add to .gitignore: build files and artifacts (node_modules/, dist/, build/, *.o, *.class), dependency files (vendor/, package-lock.json — depends on team strategy), IDE and editor files (.idea/, .vscode/, *.swp), operating system files (.DS_Store, Thumbs.db), files with secrets (.env, config/local.json, *.pem, *.key), log files (*.log, logs/) and temporary files (*.tmp, *.bak).
Syntax: each line is a rule. An asterisk replaces any number of characters: *.log ignores all log files. A trailing slash means a directory: node_modules/ ignores the entire folder. An exclamation mark cancels ignoring: !important.log will include the file even if *.log ignores it. Double asterisk — recursive path: logs/**/*.tmp ignores .tmp files at any depth inside logs/.
Structure: .gitignore in the repository root applies to the entire project. A .gitignore in a subdirectory adds rules for that directory. A global .gitignore (git config --global core.excludesfile ~/.gitignore_global) applies to all of a user's projects — convenient for OS and IDE files.
Templates: the site gitignore.io (or github.com/github/gitignore) contains ready-made templates for languages and frameworks. For a Node.js project: node_modules/, dist/, .env, .env.local, npm-debug.log*. For Python: __pycache__/, *.pyc, .venv/, .env.
Important: .gitignore doesn't remove files already tracked by Git. If a file is already in the repository, adding it to .gitignore won't help — you need to first run git rm --cached <file>, commit the removal, and only then add it to .gitignore.