learn git.
0/25 done
learn git/everyday git/course 10 of 25

.gitignore: files git should never touch

Passwords, logs, junk folders — keep them out of the photo album.

7 min read·3 sections·3 tasks
the key idea

A .gitignore file lists patterns, and any file matching a pattern never appears in git status or a commit.

01Why some files must stay out of history

Not everything in your project folder deserves to be photographed. A .env file holding your database password. A debug.log that grows every time you run the app. A node_modules folder with ten thousand files that can be rebuilt any time from a list. These files are real, they sit on your disk, and git would happily photograph them — because git cannot tell a password from a poem.

The trouble is what happens after. A file committed once is in the album forever, and history is supposed to be permanent. Commit your password and it sits in the record even after you delete it from the latest version — anyone with the history can find it. Teams have leaked secrets exactly this way. The cure is simple and cheap: teach git the rules before the first photograph, not after.

History never forgets

Deleting a secret file and committing the deletion does not scrub it from old commits. That is why the habit is: write .gitignore first, create files second.

02The .gitignore file

The rules live in one file in the top of your project, named exactly .gitignore — the dot matters. Inside it, one pattern per line. Blank lines and lines starting with # are ignored, so you can leave yourself notes.

Each pattern matches against file names. *.log — a star means anything — matches every file whose name ends in .log. A plain name like .env matches only that exact file, anywhere in the project. A folder name like node_modules/ ignores that entire folder and everything inside it.

patternwhat it ignores
.envonly the file named exactly .env
*.logany file ending in .log, anywhere
node_modules/that whole folder, at any depth
build/the build folder
# notenothing — a comment for humans
[bash]· a typical .gitignore, with comments
# secrets — never photograph these
.env

# noise the app produces
*.log

# rebuildable junk
node_modules/

03One detail people forget

The .gitignore file itself is an ordinary file, and it should be committed like one. That feels backwards until you think about who reads it: not just you, but anyone who clones your project — a teammate, future-you on a new computer, a stranger on GitHub. A committed .gitignore means the rules travel with the project automatically.

Watch what happens in the practice below: create .gitignore with *.log inside it, then create debug.log — and notice that status does not mention it at all. The file exists on disk, but to git it has become invisible. That invisibility is the whole feature.

Forcing a file in anyway

Rarely you need to commit an ignored file on purpose. git add -f file works — the f means force. It is rare enough that you may never type it.

>>

Practice

A project with real code exists. Keep the junk out of it.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks0/3 done
  1. 01.Create a file named .gitignore containing the pattern *.log

    hint: Try: echo "*.log" > .gitignore

  2. 02.Create a file called debug.log — git status should not list it.

    hint: echo junk > debug.log then run git status and look closely

  3. 03.Commit the .gitignore file.

    hint: git add .gitignore then commit