Git Ignore Tutorial - .gitignore Patterns, git check-ignore -v, Untracking a Committed File with git rm --cached, and Global Excludes (Real Output from git 2.43.0)
TL;DR
.gitignoreonly affects untracked files. If Git already tracks a file, adding it to.gitignorechanges nothing:git statuskeeps showing it as modified. Untrack it withgit rm --cached <file>, commit, and only then does the ignore rule apply. The git rm reference documents--cached, and the gitignore manual says the same in its notes.- Three places hold ignore patterns you control: the
.gitignorefiles in the working tree,.git/info/exclude(this clone only), and the file named bycore.excludesFile(every repository on your machine). Put team rules in.gitignore, editor and OS junk in the global file. - When a file is ignored and you do not know why, ask Git:
git check-ignore -v <path>prints the file, the line number and the pattern. It exits with 0 if the path is ignored and 1 if it is not, so it works in scripts. - A negation cannot rescue a file inside an ignored folder.
dist/followed by!dist/keep.txtdoes not work;dist/*followed by!dist/keep.txtdoes. I show both runs in section 5. - Not your exact case? If your problem is a secret that is already pushed, this post is only the first half; if you just want to park changes you do not want to commit yet, read git stash instead.
Table of Content
- What .gitignore does and what it does not
- Pattern syntax I use every day
- See what is ignored with git status --ignored
- Find the rule with git check-ignore -v
- Negation and the parent directory trap
- The file that is already tracked
- Force-add, info/exclude and the global file
- Cleaning ignored files
- What broke when I ran this
- Conclusion
1. What .gitignore does and what it does not
A .gitignore file is a list of patterns, one per line. Git reads it when it decides which untracked files to show in git status and which ones git add . should stage. That is the whole job. The gitignore manual puts it in one sentence: the purpose is to ensure that certain files not tracked by Git remain untracked.
Three things follow from that, and they are the source of almost every "my .gitignore is not working" question:
- It does not remove anything from the repository. A file that is in the last commit stays there.
- It does not hide tracked files. Edit a tracked file that matches a pattern and
git statusstill reports it as modified (section 6 shows the run). - It is a plain file in the repository. Because you commit it, everyone who clones gets the same rules. Rules that only you need go elsewhere (section 7).
Here are the four sources in the order Git applies them, taken from the manual:
| Priority | Source | Shared with the team? | Typical content |
|---|---|---|---|
| 1 | Command line (plumbing such as git ls-files) | No | One-off scripting |
| 2 | .gitignore in the file's folder, then each parent up to the top | Yes, if committed | Build output, logs, node_modules/ |
| 3 | $GIT_COMMON_DIR/info/exclude (normally .git/info/exclude) | No, stays in this clone | Your scratch files in this one repo |
| 4 | File named by core.excludesFile, default $XDG_CONFIG_HOME/git/ignore or $HOME/.config/git/ignore | No | .DS_Store, *.swp, editor folders |
A deeper .gitignore overrides a higher one, and inside one file the last matching line wins. If you are brand new to the repository model underneath all this, what is Git covers the working tree, index and commits first.
2. Pattern syntax I use every day
I built a throwaway repository with git 2.43.0 on 2026-12-06 and put these files in it: src/app.py, logs/app.log, logs/error.log, build/out.bin, node_modules/lib/a.js, .env, .env.example and notes.tmp. Before any .gitignore existed, git status --short listed all of them:
1$ git status --short
2?? .env
3?? .env.example
4?? build/
5?? logs/
6?? node_modules/
7?? notes.tmp
8?? src/
Then I wrote this .gitignore:
1# logs
2*.log
3logs/
4# build output
5build/
6node_modules/
7# secrets
8.env
9!.env.example
10*.tmp
The rules behind those lines, from the pattern format section of the manual:
| Pattern | Meaning | Example match |
|---|---|---|
*.log | * matches anything except a slash, so this matches at any depth | logs/app.log, a/b/c.log |
build/ | A trailing slash means directories only, at any level | build/, src/build/ |
/build/ | A leading slash anchors to the folder holding the .gitignore | only the top-level build/ |
doc/frotz/ | A slash in the middle anchors it too | doc/frotz, but not a/doc/frotz |
**/foo | Leading **/ matches in all directories | same as plain foo |
abc/** | Trailing /** matches everything inside | all files under abc/ |
a/**/b | Matches zero or more directories in between | a/b, a/x/y/b |
!pattern | Re-includes a path an earlier pattern excluded | !.env.example |
# text | Comment; escape a literal first hash as \# | not a match |
\!important.txt | Backslash escapes a leading ! | the file !important.txt |
Two edge conditions from the same page matter in practice: trailing spaces are ignored unless you escape them with a backslash, and a backslash at the very end of a pattern is invalid and never matches.
3. See what is ignored with git status --ignored
After saving the file, the same git status --short shrank to three lines:
1$ git status --short
2?? .env.example
3?? .gitignore
4?? src/
.env is gone because of .env, .env.example stayed visible because !.env.example re-included it, and .gitignore itself shows up as untracked because it is a normal file that I still have to commit. To see the ignored files too, the git status reference offers --ignored; ignored entries are marked !!:
1$ git status --short --ignored
2?? .env.example
3?? .gitignore
4?? src/
5!! .env
6!! build/
7!! logs/
8!! node_modules/
9!! notes.tmp
Notice that Git collapses an ignored directory to one line (!! build/) and never lists the files inside. That detail matters in section 5.
Ignoring also saves work, and I measured how much. I generated a node_modules/ folder with 20,000 small files (79 MB on disk) in a fresh repository and timed git status --short -uall, which lists every untracked file instead of collapsing folders:
1$ git status --short -uall | wc -l # node_modules/ not ignored
220000
3 time: 0.025 s (about 25 ms)
4$ git status --short -uall | wc -l # node_modules/ ignored
51
6 time: 0.004 s (about 4 ms)
So the listing took 25 ms without the rule and 4 ms with it, on 79 MB of files. The numbers are small because the disk is warm and the tree is small, but the shape is the point: 20,000 lines of noise against one, and roughly six times less work. The default git status without -uall collapses an untracked folder to a single line, so it stayed near 4 ms either way (3.5 to 6.7 ms across my six runs). On a real monorepo with a cold cache the gap is much larger.
4. Find the rule with git check-ignore -v
When a file is ignored and you cannot tell which line is responsible, do not read the whole file by eye. The git check-ignore reference describes -v: for each path it prints the source file, the line number and the pattern. I asked about five paths:
1$ git check-ignore -v logs/app.log .env .env.example src/app.py notes.tmp
2.gitignore:3:logs/ logs/app.log
3.gitignore:8:.env .env
4.gitignore:9:!.env.example .env.example
5.gitignore:10:*.tmp notes.tmp
6$ echo $?
70
Three observations from that output:
logs/app.logmatches both*.log(line 2) andlogs/(line 3). Git reports line 3, the last matching pattern, which is the one that decides..env.exampleis listed with the pattern!.env.example. The manual says that with-v, a path matching a negated pattern is printed too, even though it is not excluded. Do not read that line as "ignored".src/app.pyis missing from the output because no pattern matches it. When I asked about that file alone, the exit status was 1. Exit 0 means at least one path is ignored, exit 1 means none is, so you can use it in a shellif.
This one command replaces ten minutes of staring at .gitignore files, and it also tells you when the rule comes from .git/info/exclude or a global file, because the first column then shows that path.
5. Negation and the parent directory trap
The manual contains the sentence that bites most people: it is not possible to re-include a file if a parent directory of that file is excluded. Git does not even look inside an excluded directory. I tested it by creating dist/a.js and dist/keep.txt and appending this:
1dist/
2!dist/keep.txt
1$ git check-ignore -v dist/keep.txt
2.gitignore:12:dist/ dist/keep.txt
The path is still ignored by line 12, dist/. The negation on the next line had no effect. The fix is to ignore the contents of the folder instead of the folder, so Git has to look inside:
1dist/*
2!dist/keep.txt
1$ git check-ignore -v dist/keep.txt dist/a.js
2.gitignore:13:!dist/keep.txt dist/keep.txt
3.gitignore:12:dist/* dist/a.js
Now keep.txt matches the negated pattern, so it is tracked normally, and a.js is still ignored. This is the standard way to keep an otherwise empty folder in a repository, which Git cannot do on its own because it tracks files, not folders.
6. The file that is already tracked
This is the one that costs people an afternoon. I committed a file called config.local containing API_KEY=abc, then added *.local to .gitignore and changed the file:
1$ git status --short
2 M .gitignore
3 M config.local
config.local is reported as modified. The ignore rule has no effect because Git tracks the file. git check-ignore agrees with that, in its own confusing way: by default it does not look at tracked files at all.
1$ git check-ignore -v config.local
2$ echo $?
31
4$ git check-ignore -v --no-index config.local
5.gitignore:11:*.local config.local
6$ echo $?
70
The first call prints nothing and exits 1, so it looks as if the rule is wrong. The --no-index flag from the manual makes it skip the index lookup and test the pattern alone, which shows the rule is fine and the problem is the tracking. The fix is git rm --cached, which the git rm reference describes as removing the path from the index only and leaving the working tree copy alone:
1$ git rm --cached config.local
2rm 'config.local'
3$ git status --short
4 M .gitignore
5D config.local
6$ git commit -qam "stop tracking config.local"
7$ git status --short
8$ ls config.local
9config.local
10$ git ls-files
11.env.example
12.gitignore
13src/app.py
The file is still on disk, but git ls-files no longer lists it and git status is clean. For a directory use git rm -r --cached <dir>. Two cautions I want you to keep:
- The old content is still in history.
git rm --cachedremoves the file from the next commit onwards, not from the commits you already made. If it held a real secret, rotate the secret; rewriting history is a separate job (see git reset for the local part, and the git commit --amend post if the commit is the latest one and has not been pushed). - Teammates lose the file on their next pull. When they pull the commit that removes it, Git deletes the tracked file from their working tree. Tell them to back it up first.
7. Force-add, info/exclude and the global file
Force-adding. A plain git add of an ignored file is refused. This is the real message from git 2.43.0:
1$ git add dist/a.js
2The following paths are ignored by one of your .gitignore files:
3dist/a.js
4hint: Use -f if you really want to add them.
5hint: Turn this message off by running
6hint: "git config advice.addIgnoredFile false"
7$ echo $?
81
9$ git add -f dist/a.js
10$ git status --short
11 M .gitignore
12A dist/a.js
13?? dist/keep.txt
-f (see git add) stages it anyway, and from then on the file is tracked, so section 6 applies to it. I use -f only for generated files that must be committed on purpose, such as a lock file the project's ignore rules were too wide for.
Personal rules for this clone. I appended scratch.txt to .git/info/exclude. Nobody else sees the line, and git check-ignore -v names the file:
1$ git check-ignore -v scratch.txt
2.git/info/exclude:7:scratch.txt scratch.txt
Personal rules for every repository. Editor and operating system files do not belong in a project's shared .gitignore. I wrote .DS_Store and *.swp to a file and pointed core.excludesFile at it with git config:
1$ git config --global core.excludesFile /tmp/gi/global_ignore
2$ git config --get core.excludesFile
3/tmp/gi/global_ignore
4$ git check-ignore -v .DS_Store
5/tmp/gi/global_ignore:1:.DS_Store .DS_Store
On your laptop use a path such as ~/.config/git/ignore; that is the default location and works without setting anything, as long as $XDG_CONFIG_HOME is unset or empty and Git falls back to $HOME/.config/git/ignore. For the language and framework starter files, the GitHub guide on ignoring files explains the same three levels and how to start from a template.
| Where to put the rule | Command or file | Who is affected |
|---|---|---|
| Whole team | .gitignore committed in the repo | Everyone who clones |
| Just me, this repo | .git/info/exclude | This clone only |
| Just me, every repo | core.excludesFile | All my repositories |
8. Cleaning ignored files
Ignored files are not backed up by Git, so they are exactly what you lose with a careless clean. The git clean reference has -X, which removes only ignored files, and -n for a dry run. Always do the dry run first. In my demo repository it listed this:
1$ git clean -ndX
2Would remove .DS_Store
3Would remove .env
4Would remove a/
5Would remove build/
6Would remove config.local
7Would remove dist/a.js
8Would remove logs/
9Would remove node_modules/
10Would remove notes.tmp
11Would remove scratch.txt
Look at that list as a real-life warning: it contains .env and config.local, the two files with credentials in them, and config.local is the file I untracked in section 6, so the only copy Git keeps is the old one in history. git clean -fdX would delete all of this without a prompt. I use -ndX to see what a rebuild would throw away, and I do not run the real thing on a project where the ignored folder holds data I cannot regenerate.
9. What broke when I ran this
- My first
git rm --cached .envfailed. I tried it on.envout of habit, and because that file was ignored and never tracked, Git answeredfatal: pathspec '.env' did not match any files.git rm --cachedonly works on tracked paths; an ignored, untracked file needs nothing, which is the point of the ignore rule. git check-ignoreprinted nothing for a file I knew was covered by a rule. That wasconfig.localin section 6. The tool skips tracked files by default, so empty output with exit status 1 meant "not applicable because tracked", not "no rule".--no-indexshowed the real answer.- The
dist/plus!dist/keep.txtpair did nothing. I expected the file to come back, andcheck-ignore -vshowed line 12 still deciding. Switching todist/*fixed it. - The plain
git add dist/a.jsreturned exit status 1 with three hint lines. In a script that would stop a pipeline, so usegit add -fon purpose or handle the exit status. - The
!!list never showedlogs/app.log. Git collapses an ignored folder to!! logs/. If you want the individual files, rungit ls-files -o -i --exclude-standard, which lists every ignored file individually (the dry run in section 8 shows the same set at folder level).
10. Conclusion
My checklist is short. Write the .gitignore before the first git add ., not after; keep a .env.example with the variable names and !.env.example so newcomers know what to fill in; put editor files in the global excludes file instead of every project; and when something is ignored for no obvious reason, run git check-ignore -v before reading any file. If a path is tracked, remember that every ignore rule is silent about it, and that the only cure is git rm --cached plus a commit. For the same reason, run git status and git diff before every commit and look at the file list, not only the content.
The official walkthrough of the same ideas is the Pro Git chapter on recording changes to the repository, which has the ignoring files section. This post is the written companion of Day 17 of the DevOps Master Course on YouTube; the whole course lives in the free Git and GitHub blueprint course.
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.