Ignoring files and good habits


You clone a project called weather-app, run npm install, and check the status of your new repo.
$ git status
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
dist/
.envThree folders and files show up as untracked, and none of them belong in a commit. node_modules/ is thousands of files that npm install regenerates from package.json in seconds. dist/ is build output that gets rebuilt from your source every time. And .env holds an API key. None of that should ever land in your project's history, and typing git add . without thinking about it is how it happens anyway.
Telling Git what to ignore
A .gitignore file lists patterns for files and folders Git should never track. You create it once at the root of your project, the same place you ran git init when you set the repository up in Your first repository, and from then on, git status and git add . quietly skip anything that matches.
# .gitignore
node_modules/
dist/
.envEvery line is a pattern: a plain folder name like dist/ ignores that whole folder, anywhere it appears in the project. Commit the .gitignore file itself. It is small, it is useful to everyone who clones the project, and it belongs in history the same way your source code does.
$ git status
On branch main
nothing to commit, working tree cleanWith the patterns in place, git status goes quiet. That is exactly the point: the folders and files you never want to commit stop showing up as noise, so the things that do show up are the ones that actually matter.
.gitignore file lists what Git should never track: node_modules/, dist/, and .env are the usual suspects in almost any project. Commit the .gitignore file itself so everyone who clones the project gets the same clean status. I wasted a whole afternoon once staging thousands of dependency files by accident before I learned this one. Never commit secrets
An API key, a database password, or a signing certificate should never appear in a commit. That holds for a private repository as firmly as a public one, and it holds from the very first commit onward. Put secrets in a file like .env, add that file to .gitignore on day one, and load the values into your app from there instead of writing them into your source files.
# .env
WEATHER_API_KEY=sk_live_9f8e7d6c5b4a# .gitignore
.envFollow this rule automatically, every time, with no exceptions. A key sitting in a commit is reachable by anyone with access to the repository, and on a public repository, by anyone at all.
.env and add .env to your .gitignore before you ever run git add. This is the one rule in this whole chapter worth treating as absolute. Adding a file to .gitignore after it's tracked
This is the trip-up that catches almost everyone at least once. You commit a file by accident, realize your mistake, add it to .gitignore (the echo line below appends the path to that file), and expect Git to forget about it. It doesn't.
$ git add config/settings.json
$ git commit -m "Add app settings"
# later, you realize this was a mistake
$ echo "config/settings.json" >> .gitignore
$ git status
On branch main
nothing to commit, working tree cleangit status shows a clean tree, but config/settings.json is still tracked and still shows up in every future git log and every clone. Ignore rules only apply to files Git does not already know about. Once a file has been added and committed, it is part of your history, and .gitignore has no opinion on files already in that history.
To actually stop tracking it going forward, tell Git to drop the file from what it follows while leaving the file on your disk:
$ git rm --cached config/settings.json
$ git commit -m "Stop tracking config/settings.json"git rm --cached <file> removes the file from Git's tracking without deleting it from your working directory. Commit that removal, and from that point on your .gitignore pattern does the job you wanted it to do from the start.
.gitignore after Git already tracks it does nothing to that file. Ignore rules only apply to files Git has never added yet. To stop tracking a file that slipped in by accident, run git rm --cached <file> and commit that change. Good habits: small commits and a clean repo
A tidy .gitignore is one half of keeping a repository worth working in. The other half is how you shape your commits. Make each commit one focused change: a bug fix, one small feature, a single refactor. If an afternoon of work touches several unrelated things, split it into several commits instead of dropping it all into one at the end of the day.
$ git add src/weather-widget.js
$ git commit -m "Fix temperature rounding in weather widget"Small commits are easier to review, easier to revert if something breaks, and easier to read back six months later when you are trying to remember why a line of code exists. The commit loop covers what makes a commit message useful; this habit is about keeping the change itself small enough that a good message is even possible to write.
.gitignore covering the folders and secrets that should never be tracked. Those two habits together are most of what makes a repository pleasant to work in. Small habits, formed early, save you a lot of cleanup later. 
