Search code examples
gitgitignore

Can you put .gitignore in subdirectory like config instead of root directory for structure reasons?


I've ton of files that go into root folder and i'm looking to clean it up.

for example

.env
.babelrc
.eslintignore
.eslintrc.json
webpack.config.js
gulpfile.js
sequelize.json

... list goes on

I want to move all of them in config folder, because that what they are.

So is it possible to have gitignore applied from sub-directory to parent.

It seems like every tool wants to jam its config file into root directory

aws, gulp, webpack, vscode, database, react, linting...


Solution

  • It may be technically possible or not (probably not in a robust way), but regardless, my answer is

    Don't.

    The root directory is really the right place for this stuff, for several reasons:

    • it just works, without brittle or complicated quirks,
    • hiding configuration in some directory will make it harder to find for people looking for it (e.g. when something breaks),
    • hiding configuration will make it even harder to even discover that the configuration files exist at all,
    • it is just the standard place for this stuff.

    You know, you just don't want people (especially yourself) to spend three hours on debugging some issue, only to discover that it was caused by well hidden config file, and that's the result I would expect.

    Hiding the configuration goes directly against the principle of least surprise, and this is a bad idea.

    "But my files get lost between configuration!"

    If your files get lost between the configuration files, then you are solving the wrong problem. You should just put your stuff in subdirectories, like src, docs, test or wherever makes sense for your case. That's the standard way and that's what people do (at least in JavaScript world, which is apparently your case, but it's really the same with any other language). Directly in the root directory live only configs, readme, and licence.

    Put configuration in package.json instead

    On the flip side, for the specific tools you mention the config can be generally contained in package file (package.json). So if the proliferation of files itches you as such, a simple solution is to move all possible configs there. The file will grow huge (it does anyway), but it may still be preferable to multiple files.