First steps · lesson 6 of 8
Commands and ideas in this lesson
A.gitignorenames and patterns Git leaves alone
B*.pyc build/patterns for kinds of file and folders
Cgit rm --cachedstop tracking, keep the file

Ignoring files

Last updated

Some files should never be committed: things a program makes, files your computer adds by itself, and passwords. In this lesson you'll write a .gitignore that keeps them out, and learn why adding a file to it afterwards doesn't take it back out of history.

By the end of this lesson you can

  • Write .gitignore patterns for files, folders and kinds of file
  • Say which kinds of file to ignore, and why
  • Stop tracking a committed file with git rm --cached, and know that history still has it

What not to commit

Ana's Kusina folder now has the recipes, a README and a small Python script, build_index.py, that makes a web page listing every recipe. It also has things that don't belong in the history:

FileWhat it isWhy not commit it
index.htmlmade by the scriptit can be made again any time; committing it means every recipe change also changes it
__pycache__/Python's cache of compiled codemade automatically, different on every computer
.DS_Store, Thumbs.dbfolder settings macOS and Windows addnothing to do with the project
adobo.md~an editor's backup copyclutter
.enva password for the photo sitea secret: once committed and shared, it's public

The rule of thumb: commit what a person wrote; ignore what a program made, what your computer added, and anything secret.

Writing .gitignore

A file called .gitignore in the repository lists what Git should leave alone, one pattern per line. Ignored files disappear from git status, and git add . skips them.

PatternIgnores
index.htmlany file called index.html, in any folder
*.pycevery file ending in .pyc (* means any characters)
__pycache__/any folder called __pycache__, and everything in it
/notes.txtnotes.txt at the top of the repository only
*~every file ending in ~
# a commentnothing: lines starting with # are notes

.gitignore itself should be committed, so everyone working on the project ignores the same things.

Ignore the clutter

This repository has twelve files and no commits. Write a .gitignore so that git status shows only what belongs, then commit.

To write the file, type edit .gitignore to open the editor, or add lines one at a time with echo, as in echo "index.html" >> .gitignore (>> adds a line to the end of a file). Run git status after each line and watch files drop out of the list.

Git sandbox
  1. Commit the recipes, the README, the script and .gitignore
  2. Keep out index.html and __pycache__/
  3. Keep out .DS_Store, Thumbs.db and adobo.md~
  4. Keep out the password in .env

Working folder

Staging area

nothing staged

Repository

0 commits on main

You are in ~/kusina, a new repository. Some of these files belong in it, and some don’t.

Commit graph

No commits yet.

notes/todo.txt is Ana's own to-do list. Whether it belongs is her choice. Some people ignore a personal notes folder; others keep notes out of the project folder altogether.

Templates

You don't have to work out every pattern yourself. GitHub keeps a collection of .gitignore templates, github/gitignore, with one for each language and tool: Python.gitignore already lists __pycache__/, *.pyc, .env and many more. Hosting sites offer to add one when you create a repository.

Files your own computer or editor makes (.DS_Store, Thumbs.db, backup files) can also go in a global ignore file that applies to all your repositories, so each project's .gitignore only lists the project's own things. Git looks for it at ~/.config/git/ignore, or wherever core.excludesFile points.

Spot the trap: ignoring a file that's already committed

.gitignore only affects files Git isn't tracking yet. If a file is already committed, adding it to .gitignore does nothing: Git keeps tracking it. Watch.

$ git add .
$ git commit -m "Add recipes"
[main (root-commit) 6f5fdbe] Add recipes
 3 files changed, 41 insertions(+)
 create mode 100644 adobo.md
 create mode 100644 index.html
 create mode 100644 sinigang.md

Working folder

no changes

Staging area

nothing staged

Repository

1 commit on main

  1. 6f5fdbe HEAD →main Add recipes
1 of 4 Ana commits everything, including the generated index.html.

Check what you learned

Which line ignores every file ending in .log?
secrets.txt was committed last week. Ana adds secrets.txt to .gitignore. What changes?
Which of these belongs in Kusina’s repository?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git