The .gitignore file is an essential component of Git's workflow. It tells Git which files and folders to ignore, preventing unnecessary or sensitive data from being tracked in your repository.
Certain files should not be included in version control because they are either:
- Temporary or system-generated (e.g., cache, build files, logs)
- Large dependencies that can be reinstalled (e.g.,
node_modules) - Personal or sensitive configuration files (e.g., API keys, environment variables)
- IDE or editor-specific files (e.g.,
.vscode/,.idea/)
Ignoring these files keeps the repository clean, reduces conflicts, and prevents security risks.
To create a .gitignore file:
- In your project root directory, create a new text file named
.gitignore. - List the files and folders you want to ignore, one per line.
- Save the file.
*→ Wildcard for matching multiple files./→ Specifies path relative to.gitignore.#→ Adds comments.
# Ignore Mac system files
.DS_Store
# Ignore dependency folders
node_modules/
venv/
# Ignore log and cache files
*.log
.cache/
# Ignore environment files
.env
# Ignore all text files
*.txtTo create a global .gitignore file (applies to all repositories):
git config --global core.excludesfile ~/.gitignore_globalThen, edit ~/.gitignore_global as you would a local .gitignore.
If a file was already committed before adding it to .gitignore, you need to remove it from tracking:
-
Untrack a single file (but keep it locally):
git rm --cached filename
-
Untrack all ignored files:
git rm -r --cached . git add . git commit -m "Updated .gitignore"
To undo git rm --cached filename, use:
git add filename