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:
| File | What it is | Why not commit it |
|---|---|---|
index.html | made by the script | it can be made again any time; committing it means every recipe change also changes it |
__pycache__/ | Python's cache of compiled code | made automatically, different on every computer |
.DS_Store, Thumbs.db | folder settings macOS and Windows add | nothing to do with the project |
adobo.md~ | an editor's backup copy | clutter |
.env | a password for the photo site | a 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.
| Pattern | Ignores |
|---|---|
index.html | any file called index.html, in any folder |
*.pyc | every file ending in .pyc (* means any characters) |
__pycache__/ | any folder called __pycache__, and everything in it |
/notes.txt | notes.txt at the top of the repository only |
*~ | every file ending in ~ |
# a comment | nothing: 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.
- Commit the recipes, the README, the script and
.gitignore - Keep out
index.htmland__pycache__/ - Keep out
.DS_Store,Thumbs.dbandadobo.md~ - 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
6f5fdbeHEAD →main Add recipes
Check what you learned
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.
- Git documentation: gitignore: the pattern format, and that ignoring doesn't affect tracked files
- Git documentation: git-rm, --cached: removing a file from the index only
- Pro Git: Ignoring Files: examples of patterns
- github/gitignore: a collection of .gitignore templates: ready-made files for Python, Node, editors and operating systems