1

I am new to git and try to understand gitignore. So far I have a directory I want to exclude called build, except files with the name BUILD.

build/**
!build/**/BUILD

But it seems the BUILD file is excluded as well. Does anyone know what I am doing wrong?

resource/obj1/BUILD
resource/obj2/BUILD
resource/obj3/BUILD
Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
  • From the [gitignore documentation](https://git-scm.com/docs/gitignore) - A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on. – Vasil Velichkov Feb 21 '20 at 03:31
  • So it should work, since I use `!` to exclude the build files? – Daniel Stephens Feb 21 '20 at 03:41
  • Not sure whether I understand what exactly you are trying to achieve. Try `!build/BUILD` or `!build/*BUILD` – Vasil Velichkov Feb 21 '20 at 03:45
  • I want to exclude everything in `build` except the files `BUILD`. Both don't work unfortunately :-/ – Daniel Stephens Feb 21 '20 at 03:47
  • You can have only one file with a given name in a given directory, so why are saying "files BUILD"? Try clarifying th question and give some more examples as to me it's unclear what your problem is. – Vasil Velichkov Feb 21 '20 at 03:49
  • I wrote down more examples. I can't clarify it more :-/ – Daniel Stephens Feb 21 '20 at 03:53
  • Sorry, it's still unclear. Can you list all files and provide the actual output together with the expected output. – Vasil Velichkov Feb 21 '20 at 03:56
  • It's the typical request. "Exclude everything in a directory except". But I think I found my problem. http://www.randallkent.com/2010/04/30/gitignore-not-working/ – Daniel Stephens Feb 21 '20 at 03:58
  • 1
    It's great that you've found an answer. You should've told us that you've added everything in the build directory to your repo and you see the files as `not staged` and not as `untracked`. Yes you can't ignore a file that is under git source control, and you first need to remove it and then ignore it. – Vasil Velichkov Feb 21 '20 at 04:04

2 Answers2

4

You need to exclude (whitelist) folders firsts, then BUILD/ content:

build/**
!build/**/
!build/**/BUILD/**

See ".gitignore exclude folder but include specific subfolder"

  • build/** ignores files and folders, so build/aFolder/ is ignored
  • !build/**/BUILD/** attempts to whilelist a file under build/aFolder/BUILD/

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

And: build/aFolder/ is an already ignored folder.

Hence: !build/**/BUILD/** does not apply.

Unless: you un-ignore folders first: !build/**/ (the trailing / is important):
build/aFolder/ is no longer ignored, even though build/aFolder/<files> are still ignored by build/**.

Then: the !build/**/BUILD/** can apply to the folder BUILD/ content (its files), because the folders !build/aFolder/ and !build/aFolder/BUILD/ are not ignored.


Check if this is working then with:

git check-ignore -v -- build/aFile
git check-ignore -v -- build/path/to/BUILD/aFile

The first command should return a .gitignore rule line.
The second command should not return anything, since the file should not be ignored.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I try to understand why I need the second line. Do you have a reference for me? From a logical standpoint only 1 and 3 would be sufficient to me – Daniel Stephens Feb 21 '20 at 13:59
  • @DanielStephens Sure. 1/ ignores files and folders, and once a folder is ignored... any exclusion line (like 3/) within that folder is ignored as well. Hence 2/. See more at https://stackoverflow.com/a/20652768/6309: "**It is not possible to re-include a file if a parent directory of that file is excluded.**". – VonC Feb 21 '20 at 14:28
  • @DanielStephens I have edited the answer to include those details. – VonC Feb 21 '20 at 14:36
0

You don't have to specify the files if you want to exclude entire directory.

build

and git will ignore build directory.

Donato Yi
  • 1
  • 1