Some files never belong in a commit: node_modules/ (packages anyone can reinstall), logs, and .env files with passwords and keys. A key that lands in a commit is copied to everyone who gets the repository.
.gitignore is a text file in the project folder that lists them, one pattern per line. nano .gitignore opens it in the editor panel, and makes it if it doesn't exist yet. Git leaves every file that matches a pattern alone: an ignored file never shows up in git status.
node_modules/ ← a folder and everything in it
*.log ← every file ending in .log
secrets.json ← one fileBefore a .gitignore:
git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
debug.log
node_modules/
nothing added to commit but untracked files present (use "git add" to track)After one with node_modules/ and *.log:
git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore ← debug.log and node_modules/ are gone
nothing added to commit but untracked files present (use "git add" to track)When git ignores a file you didn't expect, ask it which line is responsible:
git check-ignore -v debug.log
.gitignore:2:*.log debug.log ← line 2, pattern *.logWith a good .gitignore, git add . is safe: it stages every change in the folder (. means "here") except ignored files.
Objectives
- Keep
.envout of git. - Find out why
index.htmlnever shows up. - Commit the home page, and nothing secret.