Search code examples
gitantgitignore

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.


Solution

  • 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.