A .gitignore file looks like a simple list of names, and most of the time it behaves like one. Then a log file the list says to keep goes missing, a build folder deep in the source tree disappears from commits, or a file added to the list keeps showing up as modified. Each of those comes from a specific rule in Git’s pattern format. The examples below were run with Git 2.43 against a test repository, using git check-ignore -v, which reports exactly which line of which file decided each path.
What .gitignore does and does not do
Untracked files only
The Git documentation opens with the definition and its most important limit in two sentences. A gitignore file specifies intentionally untracked files that Git should ignore. And: “Files already tracked by Git are not affected.”
Every confusing case in this post traces back to one of those two sentences. The first says the patterns decide what git status and git add . see. The second says a file Git already knows about is outside their reach entirely.
Where patterns come from
There is more than one ignore file. The documentation lists them from highest priority to lowest:
- Patterns given on the command line, for the commands that accept them
.gitignorefiles in the same directory as the path or any parent, up to the top of the repository.git/info/exclude, private to one clone and never committed- The file named by the
core.excludesFilesetting, for patterns that apply to every repository on the machine
The .gitignore in the repository is the shared one. Editor backup files and operating-system clutter belong in the personal ones, so each developer’s habits stay out of the project’s file.

The test repository
Files and rules
The test repository held build output at the top level and a source directory with its own build folder, log files, a .env file and its example, a node_modules folder and temporary files at several depths. The first .gitignore:
# build output, only at the top level
/build/
*.log
!logs/keep.log
.env
node_modules/
**/*.tmp
What check-ignore reported
$ git check-ignore -v build/out.o src/build/gen.c logs/app.log logs/keep.log \
debug.log .env .env.example a/b/c/deep.tmp a/top.tmp notes.txt
.gitignore:2:/build/ build/out.o
.gitignore:3:*.log logs/app.log
.gitignore:4:!logs/keep.log logs/keep.log
.gitignore:3:*.log debug.log
.gitignore:5:.env .env
.gitignore:7:**/*.tmp a/b/c/deep.tmp
.gitignore:7:**/*.tmp a/top.tmp
Each line gives the file, the line number and the pattern that decided it. Paths that no pattern touched, src/build/gen.c, .env.example and notes.txt, do not appear. The logs/keep.log line looks like it is ignored, but the pattern that matched starts with !, which means the opposite. git status confirmed it: keep.log showed up as an untracked file, ready to add.
check-ignore -v is the tool to reach for whenever a file is mysteriously ignored or mysteriously not. It names the culprit.
Slashes decide where a pattern applies
Three versions of one word
The documentation’s rules for the slash are short. A slash at the beginning or in the middle makes the pattern relative to the directory level of the particular .gitignore file. A slash at the end makes it match directories only. No slash at all lets it match at any depth.
The test, with a top-level build/ folder, a nested src/build/ folder and a plain file called buildfile:
| Pattern | build/out.o |
src/build/gen.c |
buildfile |
|---|---|---|---|
build |
ignored | ignored | not ignored |
build/ |
ignored | ignored | not ignored |
/build |
ignored | not ignored | not ignored |
Without a leading slash, build caught the nested source folder too, which in a real project might hold generated code that is meant to be committed. The leading slash anchors it to the top level. buildfile was safe in every case, because a pattern without wildcards matches whole names only.
Directories only
The trailing slash matters when a file and a folder could share a name. logs/ matches a directory called logs but not a file called logs, while logs alone matches either.

Wildcards and double stars
One star stops at a slash
The documentation says an asterisk matches anything except a slash, a question mark matches any one character except a slash, and ranges such as [a-z] match one character from the set. *.log in the test therefore matched debug.log at the top and logs/app.log one level down, because a pattern with no slash in it applies at every level anyway. The star never had to cross a directory.
Two stars cross directories
** is the exception, with three positions documented:
- Leading,
**/foo: matchesfooin all directories. - Trailing,
abc/**: matches everything insideabc. - Middle,
a/**/b: matchesa/b,a/x/b,a/x/y/band so on, zero or more directories.
In the test, **/*.tmp matched a/top.tmp one level down and a/b/c/deep.tmp three levels down. For this particular pattern *.tmp would have done the same, since it has no slash. The double star earns its place in patterns that do contain a slash, such as docs/**/*.html, which a single star could not express.
Negation, and the rule that breaks it
The exclamation mark
A pattern starting with ! re-includes something an earlier pattern excluded. The documentation’s own example keeps one hand-maintained file among generated ones:
*.html
!foo.html
The order matters: within a file the last matching line wins, so the ! line has to come after the line it overrides.
Ignored directories cannot be reopened
The documentation then adds the sentence that causes the most trouble: it is not possible to re-include a file if a parent directory of that file is excluded. The test, two versions of the same intent:
logs/
!logs/keep.log
check-ignore reported .gitignore:1:logs/ logs/keep.log. The whole directory was excluded, so Git never looked inside it, and the ! line never ran.
logs/*
!logs/keep.log
This time the report was .gitignore:2:!logs/keep.log, and the file was included. The difference is between ignoring the directory and ignoring everything inside it. The second form leaves the directory open, so individual files can be let back in. The documentation’s longer example, which keeps only one subdirectory of a project, is built entirely on that pattern: /*, then !/foo, then /foo/*, then !/foo/bar.

The file that keeps showing up
Already tracked
The second sentence of the definition causes the most common complaint. In the test, notes.txt was committed first, then added to .gitignore, then edited:
$ git status --porcelain
M notes.txt
Still reported as modified. Git was already tracking it, and ignore rules do not apply to tracked files.
git rm –cached
The documentation’s fix is to remove the file from the index with git rm --cached, which stops tracking it without deleting the copy on disk:
$ git rm --cached notes.txt
$ git status --porcelain
D notes.txt
The D is a staged deletion: the next commit removes the file from the repository from that point on, while the local file stays where it is and is now ignored. It is still in the history of older commits. A .env file with real passwords that was committed once needs the passwords changed, not just the file ignored.
Trailing spaces
One more detail from the format rules: trailing spaces are ignored unless escaped with a backslash. A line secret.txt with a stray space at the end still matched secret.txt in the test. Git forgives the stray space; a pattern that really needs one has to escape it.
Seeing everything Git is ignoring
Three listing commands
check-ignore answers for a named path. Three other commands list everything at once, run here on a small repository ignoring *.log and /build/:
$ git status --porcelain --ignored
?? .gitignore
?? main.c
?? notes.swp
!! build/
!! logs/
!! marks ignored paths, shown next to the untracked ?? ones. git ls-files --others --ignored --exclude-standard lists the ignored files one by one, build/out.o and logs/app.log. And git clean -ndX previews what a cleanup would delete, only the ignored paths:
$ git clean -ndX
Would remove build/
Would remove logs/
The -n makes it a dry run. Without it, git clean -dX really deletes them, which is a quick way to reset a build but also a quick way to lose an ignored .env file.
The personal ignore files at work
Adding *.swp to .git/info/exclude worked the same way as a .gitignore line, and check-ignore -v named the file it came from: .git/info/exclude:7:*.swp. A global file set with git config core.excludesFile /tmp/gignore containing *.bak was reported the same way: /tmp/gignore:1:*.bak. Neither file is ever committed, so neither affects anyone else.
Adding an ignored file anyway
git add logs/app.log refused with a message naming the reason, “The following paths are ignored by one of your .gitignore files,” and a hint to use -f. git add -f added it, and from that moment it was tracked, which means the ignore rule no longer applies to it, exactly as the definition says.
Sources
- Git documentation, gitignore: https://git-scm.com/docs/gitignore
