Hone

Lessons · Git · files git should not watch

Files Git should never track

A .gitignore file lists patterns for files that should not be tracked: build output, logs, secrets, editor junk. Ignored files never show as untracked.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Generated files change on every build and secrets must never enter history. One committed .env is in every clone forever, even after you delete it.

How to think about it

Write .gitignore first, commit it so the whole team shares it, then check git status: only real source should show. A file already committed needs git rm --cached, later in this stage, to stop being tracked while staying on disk.

Worked example

printf 'build/\n*.log\n' > .gitignore
Patterns for files that should never be tracked.
mkdir -p build && echo out > build/app.js && echo log > run.log
Create some of them.
git status --short
Only .gitignore shows: the build folder and the log are invisible.
git add .gitignore && git commit -m "Ignore build output and logs"
The ignore file itself is tracked, so everyone shares it.

Your turn

Stop tracking a file that was committed before it was ignored.

git rm --cached 

The trap

.gitignore affects untracked files only. A file already committed stays tracked until git rm --cached removes it from the index, which is a later lesson in this stage.

Practise files git should not watch on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.