Sub topic 13
Keeping Files Out with .gitignore
This chapter assumes you can run basic Git commands, have a repository with at least one commit, and know the difference between the working tree and the staging area. Here you will learn how a .gitignore file keeps build output, logs, and other generated files out of your history.
Why ignore files
Not every file in your project folder belongs in history. Log files, compiled output, editor settings, and downloaded dependencies can be rebuilt or are specific to your machine. If they are committed, they add noise to git status, bloat the repository, and cause pointless conflicts between collaborators.
A .gitignore file is a plain text file that lists patterns. Git does not offer matching untracked files for staging, and git add . skips them.
Creating a .gitignore
Create the file in the root of the repository, then commit it so the rules are shared with everyone who clones the project.
printf "*.log\nbuild/\n" > .gitignore
git add .gitignore
git commit -m "Ignore log files and build output"You can check the effect with a test file and git status.
echo "debug output" > app.log
mkdir build
echo "compiled" > build/app.bin
git status --shortThe output does not list app.log or build/. Only changes to files that Git is allowed to track appear.
Pattern rules
Each line is one pattern. Blank lines are skipped, and lines starting with # are comments.
| Pattern | Meaning |
|---|---|
| `*.log` | Any file ending in `.log`, in any directory |
| `build/` | A directory named `build`, and everything inside it |
| `/notes.txt` | Only `notes.txt` in the same directory as `.gitignore` |
| `!keep.log` | Do not ignore `keep.log`, even if an earlier pattern matched it |
| `docs/*.tmp` | `.tmp` files directly inside `docs` |
Later patterns override earlier ones. A pattern with a leading ! only works if Git can still reach the file. If you ignore a whole directory, you cannot re-include a file inside it.
Common errors
The most frequent mistake is adding a file to .gitignore after it was already committed. Ignore rules apply only to untracked files, so Git keeps tracking the file. To stop tracking it without deleting it from disk, remove it from the index.
git rm --cached app.log
git commit -m "Stop tracking app.log"Other things to watch for:
- A pattern with a trailing slash, such as
build/, matches only directories. A file namedbuildis not matched. - Trailing spaces in a pattern are treated as part of it, so
*.logwill not match. - Secrets that were already committed stay in history. Ignoring them later does not remove them, so rotate the credentials.
Exercise
Exercise
You committed secrets.env last week, and today you add secrets.env to .gitignore. Git still shows changes to it in git status. Why does this happen, and what command stops Git tracking it while keeping the file on disk?
Show answer
.gitignore only affects untracked files, and secrets.env is already tracked because it was committed. Run git rm --cached secrets.env and commit the result. Git then stops tracking the file, and the ignore rule takes effect. The earlier commits still contain the file, so any credentials in it should be rotated.