Ignoring files with .gitignore
Keep generated files, secrets and local data out of Git with .gitignore: how patterns match, how to check them, and why a tracked file ignores your ignore rule.
Ignoring files with .gitignore
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A .gitignore file lists patterns for files Git should treat as invisible, which keeps generated files, local data and secrets out of commits.
- Ignoring only affects untracked files. A file that is already tracked stays tracked until you remove it with git rm --cached.
- git check-ignore -v shows which pattern in which file is ignoring a path, which is the way to debug a rule that does not behave.
- A trailing slash matches only directories, and ! re-includes a path, but not if a parent directory is already ignored.
- Ignoring a file does not remove a secret already committed. Once pushed, treat it as leaked.
Files that should never be in history
Run python3 notes.py add "buy milk" in tinynotes and a new file, notes.txt, appears. That’s the app’s data, not its code. Check:
$ git status -s
?? notes.txt
If you run git add . without thinking, your personal shopping list becomes part of the project history, and a teammate’s own notes.txt will clash with it. Three kinds of file generally don’t belong in a repository:
- Generated files: compiled output, caches,
node_modules, anything a command can rebuild. - Local data and editor clutter: your notes file,
.DS_Store, IDE settings. - Secrets:
.envfiles, API keys, passwords.
The third kind is the dangerous one, and the reason is in the history lesson: once a secret is committed, it lives in every clone and every old snapshot. Prevent it by never staging it in the first place.
The .gitignore file
A plain text file named .gitignore in the project root. Each line is a pattern. Git won’t show matching untracked files in git status and git add . skips them. I created one for tinynotes:
$ cat .gitignore
notes.txt
__pycache__/
.env
*.log
$ git status -s
?? .gitignore
notes.txt has disappeared from the list. The .gitignore itself is untracked, and it should be committed so your teammates get the same rules:
$ git add .gitignore
$ git commit -m "Ignore notes.txt and Python cache files"
[main 27adccf] Ignore notes.txt and Python cache files
1 file changed, 4 insertions(+)
create mode 100644 .gitignore
To see what’s being hidden, git status --ignored -s marks ignored files with !!:
$ git status --ignored -s
?? .gitignore
!! notes.txt
How patterns match
notes.txtmatches a file of that name anywhere in the project.*.logmatches any file ending in.log.*is a wildcard within one path segment.build/with a trailing slash matches only directories./todo.txtwith a leading slash matches only at the project root.#starts a comment, and a line starting with!re-includes something an earlier line ignored.- Later lines win over earlier ones.
When a rule doesn’t do what you expect, ask Git which rule applied:
$ git check-ignore -v logs/a.log logs/keep.log build/out.o
.gitignore:1:*.log logs/a.log
.gitignore:2:!logs/keep.log logs/keep.log
.gitignore:3:build/ build/out.o
Each line says file, line number and pattern, then the path. A path that isn’t ignored prints nothing, and the command exits with status 1.
The negation trap
That output came from a rule set where !logs/keep.log rescued one file from *.log, and it worked: git status -s -uall listed logs/keep.log as untracked while logs/a.log stayed hidden. Now the version that surprises people. I replaced the rules with:
logs/
!logs/keep.log
and git check-ignore -v logs/keep.log printed .gitignore:1:logs/ logs/keep.log. The file was still ignored. Git never looks inside an ignored directory, so a negation for a file in it has no effect. The fix is to ignore the contents instead of the directory: logs/* followed by !logs/keep.log. I ran that variant and logs/keep.log came back as untracked while logs/a.log stayed ignored.
Why your ignore rule is being ignored
This is the question behind most .gitignore confusion. .gitignore only affects files Git isn’t tracking yet. Here is a scratch repository where I had already committed log files with git add -A, then added *.log to .gitignore:
$ git status -s
?? .gitignore
$ git ls-files
app.log
build/out.o
logs/a.log
logs/keep.log
main.c
git status shows no log changes, but git ls-files still lists the logs as tracked, so Git keeps watching them, and any edit to them would show up as a modification. The fix is to untrack them without deleting the files on disk:
$ git rm --cached app.log logs/a.log logs/keep.log
rm 'app.log'
rm 'logs/a.log'
rm 'logs/keep.log'
--cached removes the file from the staging area only. After committing, the logs stayed on disk, ignored. The commit does delete them from the new snapshot, but they remain in every old commit. For a secret, that is a leak: untracking is not enough, and you’d need to rotate the secret. A later lesson covers scrubbing history.
Personal and global ignores
Rules the whole team needs go in .gitignore. Rules only you need don’t belong there. Two other places hold them:
.git/info/exclude: same syntax, applies to this repository on your machine only. I addedscratch.txtto it andcheck-ignore -vreported.git/info/exclude:7:scratch.txt.- A global ignore file, set with
git config --global core.excludesFile <path>. Put things like.DS_Storethere. I did, andcheck-ignore -vcredited that file.
Forcing an ignored file in
git add x.log on an ignored file refuses, listing the file and the hint “Use -f if you really want to add them”. git add -f x.log stages it anyway. Rarely what you want, but handy for a committed template such as .env.example that would otherwise match an ignore rule.
What the video covers
The embed is Net Ninja’s “Git Crash Course #9 - .gitignore File”, about six minutes, from a series published around September 2025. It is the one video in the series devoted to the file, and the series description says it teaches setting up a repository, staging, committing and branching. I couldn’t read a transcript or chapters, and the description says nothing specific about this episode, so I can’t confirm which patterns it demonstrates. The tracked-file trap, check-ignore, negation and the global ignore file on this page are probably not in it, and come from the Git documentation and my own runs. The channel is English-language, judged from the title, description and channel. It replaces a slice of under two minutes from Traversy Media’s January 2025 crash course, which is linked under deeper at its chapter, together with DevOps Journey’s longer video from about 2020.
Where this fits
Your history now holds only what should be there. Next: what to do when you change your mind about an edit, before or after staging it. See undoing uncommitted changes.
Check yourself
You added *.log to .gitignore, but debug.log still shows as modified in git status. Why?
The file is already tracked, and .gitignore only applies to untracked files. Run git rm --cached debug.log to stop tracking it, then commit.
What does git check-ignore -v notes.txt print?
The file, line number and pattern that ignores notes.txt, for example .gitignore:1:notes.txt. It prints nothing and exits 1 if the path isn't ignored.
Why does logs/ followed by !logs/keep.log not keep logs/keep.log?
Git doesn't look inside an ignored directory, so a negation for a file within it can't take effect. Ignore the contents with logs/* instead.
Where do you put a pattern for your own editor's scratch files that the rest of the team shouldn't have to ignore?
In .git/info/exclude for one repository, or in a global ignore file named by core.excludesFile for all of them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.