.gitignore negation not working
Asked Answered
S

2

15

I'm using msysgit 1.9.4.

My .gitignore file, at the root of the repo, looks like this:

build/
a/b/
!a/c/d/e/**/build/

My intent is that, all build/ directories are ignored, unless they are subdirs (any depth) of a/c/d/e/. But a/c/d/e/f/build/ is still getting ignored by Git.

What am I missing here?

Saurischian answered 18/6, 2014 at 23:47 Comment(9)
possible duplicate of .gitignore exclude folder but include specific subfolderEvslin
Is it possible you have other git ignore rules getting picked up? In addition to picking up rules from the gitignore file, it could potentially read rules from other gitignore files within the tree, from $GIT_DIR/info/exclude, or by the configuration variable core.excludesfile. See git-scm.com/docs/gitignore#Carvelbuilt
@Chris, thanks, but it's not a duplicate, I'm not excluding the folder from which the folder I want included is being ignored. edit: To clarify, a/c/d/e/f/ is included, but a/c/d/e/f/build/ is ignored when I want it included.Saurischian
@jkyako, no, there are no other gitignore files within the repo, and .git/info/exclude is empty, and core.excludesfile is not assigned.Saurischian
@ErikaE, what does git check-ignore -v a/c/d/e/f/build/ show? What about the same command with a file in build/ instead of the directory itself?Evslin
The former shows a line number, the ! rule, and a/c/d/e/f/build. The latter (specific file) has no output. Maybe this is a case of, it's picking up the new ignore exclusion properly, but I have to do something extra (I ran 'git add .') to pick up previously ignored files?Saurischian
I think what I needed to do is run 'git add -f'? From reading related questions, I now understand that Git maybe saw that nothing in the /a/c/d/e/f/build/ directory had changed since the last commit...because nothing had, by date modified on the files themselves -- except their ignore status.Saurischian
@ErikaE if you're using git status to see what has changed; that does it by file date/time (I guess it would be too slow to diff every file in a large project whenever a status command is issued)Cinemascope
@ErikaE did you find a solution for this?Buran
H
8

The general rule of gitignore is simple:

It is not possible to re-include a file if a parent directory of that file is excluded.

To exclude files (or all files) from a subfolder of an ignored folder build, you need to exclude the folder first, then the files:

I have tested:

**/build/
a/b/
!a/c/d/e/**/build/     <== whitelist folder first
!a/c/d/e/**/build/**   <== then the files
Hypochondrium answered 9/2, 2022 at 21:27 Comment(2)
I agree that this is "simple" in the sense of being easy to understand after you learn it. But it's not really "simple" in the sense that it's intuitive or easy to guess before you learn it. (To be clear: I'm complaining about the design of git and not your answer! :)Opportune
@Opportune I agree. It took me a while before getting the hang of it.Hypochondrium
P
0

For example: You have ignored the /node_modules/ folder. But you want to add a single file which you need to patch. This is the file to whitelist: /node_modules/x/y/z/file.js

Add the following TO THE END of your .gitignore file:

/**
!/node_modules
!/node_modules/x
!/node_modules/x/y
!/node_modules/x/y/z
!/node_modules/x/y/z/file.js

The first line (/**) is important - no idea how it works :-)

Peppery answered 17/10, 2023 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.