10

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?

ellriley
  • 605
  • 2
  • 7
  • 21
  • possible duplicate of [.gitignore exclude folder but include specific subfolder](http://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder) – ChrisGPT was on strike Jun 19 '14 at 00:17
  • 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 http://git-scm.com/docs/gitignore# – jkyako Jun 19 '14 at 00:17
  • 1
    @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. – ellriley Jun 19 '14 at 00:21
  • @jkyako, no, there are no other gitignore files within the repo, and .git/info/exclude is empty, and core.excludesfile is not assigned. – ellriley Jun 19 '14 at 00:23
  • 1
    @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? – ChrisGPT was on strike Jun 19 '14 at 00:25
  • 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? – ellriley Jun 19 '14 at 00:53
  • 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. – ellriley Jun 19 '14 at 01:00
  • @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) – M.M Jun 19 '14 at 01:46
  • @ErikaE did you find a solution for this? – Tigran Aug 04 '17 at 09:15

1 Answers1

4

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    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! :) – Tyler Nov 05 '22 at 23:25
  • @Tyler I agree. It took me a while before getting the hang of it. – VonC Nov 05 '22 at 23:28