Git will happily record anything in your folder. But some things must not be recorded, and others are pointless to record. The file that tells Git what to skip is .gitignore.
Three kinds of things to keep out
| Kind | Examples | Why |
|---|---|---|
| Things you can regenerate | node_modules/, build output folders | One command rebuilds them, and they’re enormous |
| Things your OS creates | .DS_Store (Mac), Thumbs.db (Windows) | Local clutter that means nothing to anyone else |
| Things nobody should see | Files with passwords or API keys | Once on GitHub, deleting them doesn’t remove them from history |
That third one matters most. Assume that a password in a public repository will be found by a bot within minutes.
Creating the file
At the top level of your repository (next to index.html), create a file named .gitignore.
my-site/
├── .gitignore ← the new file
├── images/
│ └── shima.png
├── index.html
├── script.js
└── style.cssInside, list one thing to ignore per line.
# Files created by macOS and Windows
.DS_Store
Thumbs.db
# npm's parts bin
node_modules/
# Files containing keys
.envLines starting with # are comments and are ignored by Git. Noting why a line is there helps later.
Patterns you can write
| Pattern | Meaning |
|---|---|
.DS_Store | Ignore files with this name, at any level |
node_modules/ | Ignore this folder and everything in it |
*.log | Ignore every file ending in .log (* means “anything”) |
draft/memo.txt | Ignore exactly this file in exactly this place |
!important.log | Exclude from being ignored (! means “except this”) |
You don’t need * and ! right away. Listing plain file and folder names covers most of what you need.
Check that it’s working
After writing .gitignore, run:
git statusIf the rules apply, .DS_Store and node_modules/ have disappeared from the list. If they’re still there, check the spelling or whether the file is in the right place.
Removing something you already committed
This is the biggest trap. .gitignore only skips files Git isn’t already tracking, so it has no effect on files you’ve already recorded.
To untrack them:
git rm --cached .DS_Store
git commit -m "Stop tracking .DS_Store"--cached means remove it from Git’s tracking but leave the actual file alone. For a whole folder, add -r (recursively):
git rm -r --cached node_modulesSummary of this lesson
- Anything listed in
.gitignorestops being recorded by Git - Keep out regenerable files, OS clutter, and secrets
- For already-recorded files, use
git rm --cached— leave off--cachedand you delete the real thing