.gitignore
A `.gitignore` file tells Git which files or folders to ignore, so they are never staged, committed, or shown as untracked. This is useful for build artifacts, dependency folders, and secrets.
You create a `.gitignore` file in your project root and list patterns for files you want Git to skip, such as `node_modules/` or `*.log`.
# .gitignore example
node_modules/
*.log
.envCommon patterns
Use exact filenames like `secret.env`, wildcards like `*.log`, or folder patterns like `node_modules/` to ignore entire directories such as dependencies or build output.
Ignoring already-tracked files
Adding a file to .gitignore does not untrack it if it's already committed. Use `git rm --cached filename` to stop tracking it while keeping the local copy.
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
git statusOn branch main
nothing to commit, working tree cleanAdds ignore patterns so node_modules and log files no longer appear as untracked.
git rm --cached config.env
echo "config.env" >> .gitignorerm 'config.env'Stops tracking a previously committed file and adds it to .gitignore so it stays ignored going forward.
Key points
- .gitignore lists files and folders Git should never track.
- Common entries include dependency folders and log files.
- Already-tracked files need git rm --cached to stop being tracked.
- Keeping secrets out of Git history is an important security practice.
