Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27286

git ignore directories "build" and all their contents but not "build.xml"

I am using git in a project where Ant is the build system. Since I do not wish to add "build" directories to git but need to add build.xml files I have resorted to the following two lines in my .gitignore files:

build/
!build.xml

Is there a better / more concise way ?

UPDATE: turns out build.xml was ignored due to a .gitignore in a directory higher up the path. Once that was removed, build/ correctly ignores only the build folder, not the build.xml file beside it.

Upvotes: 19

Views: 67423

Answers (3)

user16776498
user16776498

Reputation:

You can also ignore any folder starts with build like so:

**/.build
**/build

Upvotes: 3

eis
eis

Reputation: 53462

No, there is no better way, that's pretty much the way to do it. To be precise,

build/*
!build/build.xml

As you want to have the folder and ignore all other files in it except build.xml.

Note though that common directory layout for Ant is that build.xml is in the app root folder, whereas build folder only contains files created during build. See further rationale in, for example, here.


Edit. if you actually have build.xml in the app root, just

build

in .gitignore should be enough. However, it should be noted it only applies to detecting new entries, if you've already committed something to git, you'll have to remove that yourself.

Upvotes: 30

Nick
Nick

Reputation: 21

You can also just ignore /build/, that will ignore only the folder called build, no files.

Upvotes: 2

Related Questions