Skip to the workspace
Pebellslearn by building
What is a Repository  ›  Lesson

.gitignore — Files That Should Never Be Tracked

.gitignore 12 minA .gitignore file lists patterns for untracked files Git should ignore, but it cannot stop Git tracking a file that was already committed.

git github / .gitignore — Files That Should Never Be Tracked

Explore each scene at your own pace.

01 · The rule

The rule

.gitignore — Files That Should Never Be Tracked

Asha's editor drops a .DS_Store file into every folder, and she has a secrets.txt holding an API key for a recipe-search tool. Neither belongs in history. Every git status lists them as untracked, and one careless git add . would commit them. A .gitignore file stops that.

The rule

.gitignore hides untracked files from Git; it cannot un-track a tracked one.

Create a plain text file called .gitignore in the repository's top folder, one pattern per line:

# system clutter
.DS_Store

# private settings
secrets.txt
.env

# anything that ends in .log
*.log

# a whole folder
cache/

# everything inside build/ ...
build/*
# ...except this one file
!build/README.md

How patterns read:

  • secrets.txt matches a file with that name in any folder.
  • *.log matches any name ending in .log; * stands for any run of characters.
  • A trailing /, as in cache/, matches only folders.
  • A leading /, as in /notes.txt, matches only at the top of the repository.
  • ! re-includes something an earlier line excluded. It cannot rescue a file whose parent folder is itself ignored, which is why the example ignores build/*, the folder's contents, rather than build/ itself.
  • # starts a comment.

Commit the .gitignore itself, so everyone who clones the repository shares the same rules.

The key limit: Git only ignores files it is not already tracking. If secrets.txt was committed before the rule was added, Git keeps tracking it. Stop tracking it without deleting your local copy:

git rm --cached secrets.txt
git commit -m "Stop tracking secrets file"

That removes it from future snapshots, but it is still visible in older commits. If a real secret was ever committed, treat it as leaked and replace it.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.