Dmitry Belaventsev
Dmitry Belaventsev

Reputation: 6657

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:

I want to allow following:

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

Upvotes: 59

Views: 36241

Answers (3)

Abdulaziz Hamdan
Abdulaziz Hamdan

Reputation: 373

Despite being an old post, but still relevant today.

After long debugging with above solutions, still didnt work for me, but I finally got it fixed by simply executing the following command:

  • git add .\[your_filename_in_full_path] -f

Where the . is expected to be your current directory.

Saved me lot of time on debugging why my .gitignore is not working and what did i miss.

Upvotes: 5

Dmitry Belaventsev
Dmitry Belaventsev

Reputation: 6657

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

Upvotes: 75

Ding
Ding

Reputation: 3085

Try 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

I think that should work.

Upvotes: 10

Related Questions