Search code examples
gitgithubgulp

gitignore - ignore generated files in a folder, but not the folder


I have a gulp task that pipes assets into a directory. I would like to include the directory in my project, but exclude the outputted files through a .gitignore file. My gulp task looks like this:

gulp.task('styles', function() {
    return sass('app/scss/styles.scss')
    .pipe(cleanCSS())
    .pipe(gulp.dest('public/stylesheets'))
    .pipe(livereload());
});

gulp.task('scripts', function() {
    gulp.src('app/js/script.js')
    .pipe(webpack(require('./webpack.config.js')))
        .pipe(uglify())
    .pipe(gulp.dest('public/javascripts'))
    .pipe(livereload());
});

Folder structure:

app
---js
---scss
public(NEED TO KEEP THIS)
---javascripts(NEED TO KEEP THIS)
-----scrpits.min.js**(NEED TO IGNORE THIS)**
---stylesheets(NEED TO KEEP THIS)
-----styles.css**(NEED TO IGNORE THIS)**

So in .gitignore if I go:

/public

That ignores everything, which is not what I want.

I found this question: How can I add an empty directory to a Git repository? and someone suggested a "workaround" with 54 upvotes:

"Andy Lester is right, but if your directory just needs to be empty, and not empty empty, you can put an empty .gitignore file in there as a workaround..."

So now I go:

app
---js
---scss
public(NEED TO KEEP THIS)
---javascripts(NEED TO KEEP THIS)
-----scrpits.min.js**(NEED TO IGNORE THIS)**
-----.gitignore
---stylesheets(NEED TO KEEP THIS)
-----styles.css**(NEED TO IGNORE THIS)**
-----.gitignore

And took a suggestion from the accepted answer and did:

/public/!.gitignore

But still is not getting the desired result.

How can I commit an output folder to a repository for a gulp task?


Solution

  • Ok I figured it out:

    /public/**/*.js
    /public/**/*.css
    

    Essentially, any file in any folder below one level in /public should be ignored.