.gitignore

Ignore what does not belong

Lesson 7 of 20 · 9 min · Module 2: Everyday changes

Some files should never be committed: logs, downloaded libraries, editor settings, and secrets. A .gitignore file tells Git to look away.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Write .gitignore patterns
  • Check why a file is ignored
  • Commit the rules for everyone

The idea

Each line of .gitignore is a pattern. *.log matches every file that ends in .log, node_modules/ matches a folder and everything in it, and a plain name such as .DS_Store matches that name in any folder. Commit .gitignore so everyone on the project gets the same rules. git check-ignore -v file tells you which rule ignores a file. Ignoring only works for files Git does not track yet.

echo "*.log" > .gitignore
echo "node_modules/" >> .gitignore
git status

01Patterns, one per line

Each line of .gitignore is a pattern. *.log matches files ending in .log in any folder, node_modules/ matches a folder, and a plain name such as .DS_Store matches that name anywhere. Lines starting with # are comments.

echo "*.log" > .gitignore
echo "node_modules/" >> .gitignore
echo ".DS_Store" >> .gitignore
git status --short
Output
?? .gitignore
?? contact.md

02Ask why a file is ignored

git check-ignore -v prints the file, line and pattern that ignores a path: the quickest way to debug a rule that does not do what you expect.

git check-ignore -v logs/server.log
Output
.gitignore:1:*.log	logs/server.log

03Commit the rules

Commit .gitignore like any other file, so everyone who clones the project gets the same rules. GitHub offers ready-made templates for most languages when you create a repository.

Your turn

Your folder now has clutter: log files, a node_modules folder and a macOS .DS_Store file. Create a .gitignore that ignores every .log file, the node_modules/ folder and .DS_Store. Check that git status shows only .gitignore and contact.md, then commit both as Add contact page and ignore rules.

What the checks look for

  • Log files are ignored
  • node_modules and .DS_Store are ignored
  • Only .gitignore and contact.md were committed
  • Nothing is left untracked
Try it in the editor →

Take this with you

.gitignore keeps clutter, and secrets, out of git status and out of your commits.

Go a little further

Add !keep.log to .gitignore and create keep.log: the ! re-includes a file that an earlier pattern ignored. Check the result with git status --ignored.

Learn Git & GitHub for free

20 short lessons with real code and instant checks.

See the whole course →