Search code examples
gitignore

Allow only some files in subdirectories with .gitignore


I have dir structure like this:

static
    admin
    ajax_upload
    books
    css
    font
    media
    robots.txt
templates
src
build
lib

I want to ignore following direcotories:

  • lib
  • build
  • src
  • static

I want to allow following:

  • static/css/bootstrap-styled.css
  • static/css/main.css
  • static/css/font-*.css
  • static/font
  • static/media/default.png
  • static/robots.txt
  • templates

So I use the following .gitignore:

# Ignore
/lib
/src
/build
/static/*

# Allow
!/static/css/bootstrap-styled.css
!/static/css/main.css
!/static/css/font-*.css
!/static/font
!/static/media/default.png
!/static/robots.txt

But it doesn't work properly. Could you help me - what I do wrong here? TIA!

Details

Real project structure is like this:

static
    admin
        css
        img
        js
            admin
    ajax_upload
    books
    css
    font
    media
        uploads
            blog
            gallery
        default.png
    robots.txt
templates
src
build
lib

Solution

  • So, the .gitignore works for me is the following:

    # Ignore
    lib/
    src/
    build/
    static/**/*
    
    # Allow
    !static/css/bootstrap-styled.css
    !static/css/main.css
    !static/css/font-*.css
    !static/font/*
    !static/media/default.png
    !static/robots.txt