Ignore all files except a specific one in GIT

1

I want to ignore everything from the "assets" folder except for the "xmutarn.js" file, so I tried .gitignore :

!assets/script/xmutarn.js /assets

But it does not work, how can I resolve it?

My .gitignore looks like this:

## Files to ignore
*.gitignore
*.bak

## Archives to ignore
changelog*

/index.php

## Folders to ignore
.idea/
sys/session
assets/
!assets/script/xmutarn.js
    
asked by anonymous 18.10.2015 / 21:08

1 answer

5

I did not understand /assets at the end of:

!assets/script/xmutarn.js /assets

The exclamation mark that says "deny" ! ie the file on the line will not be ignored. I believe that to ignore the content of the assets folder would look something like:

assets/*

If you do this, you will ignore the entire folder (correct me if I'm wrong):

assets/

So it should look like this:

assets/*
!assets/script/xmutarn.js

Another detail, as per @DanielGomes found SOen it is necessary to add !*/ whenever it is sub-directories, apparently it does not have to be at the end as stated in link and link

Basically, we first make a blacklist and then a whitelist

The whole code should look something like:

## Files to ignore
*.gitignore
*.bak

## Archives to ignore
changelog*

/index.php

## Folders to ignore
.idea/
sys/session
assets/*

#Whitelist a seguir

!*/
!assets/script/xmutarn.js

If it does not work, it has a detail, it might be that when using assets/* it forces ignore the folders also in the case of the ./assets/script folder, so you can try to make the rules for each folder within ./assets ao instead of the whole folder.

    
18.10.2015 / 23:11