Ignore all files except one specific in GIT

Asked

Viewed 1,853 times

1

I want to ignore everything from "Assets" folder except "xmutarn.js" file, so I tried on .gitignore:

!assets/script/xmutarn.js /assets

But it doesn’t work, how can I fix it?

My . gitignore this so:

## Files to ignore
*.gitignore
*.bak

## Archives to ignore
changelog*

/index.php

## Folders to ignore
.idea/
sys/session
assets/
!assets/script/xmutarn.js
  • Please place the code here and not in an image. Because you have a space after the file name?

  • If you have ever committed the file, you will need to git rm --cached nomearquivo.js.

  • Good afternoon Vinicius, don’t post code photos, if it’s code just copy it here, so it makes it easier for people to debug exactly the way you did. Using code photos would only work if everyone had an OCR software, so avoid unless it is something pertinent to the IDE you are using, which is not the case.

1 answer

6


I didn’t understand the /assets at the end of:

!assets/script/xmutarn.js /assets

The exclamation sign means "denial" ! ie the file on the line will not be ignored. I believe that to ignore the contents of the briefcase assets would be something like:

assets/*

If you do so you will ignore the whole folder (correct me if I’m wrong):

assets/

So it should stay that way:

assets/*
!assets/script/xmutarn.js

Another detail, as the @Danielgomes found in the Soen it is necessary to add !*/ whenever it comes to sub-directories, apparently it does not need to be at the end as said in https://stackoverflow.com/a/9227991/1518921 and https://stackoverflow.com/a/8025106/1518921

Basically, first we 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 doesn’t work, there’s a detail, it may be that when using like this assets/* it force ignore the folders also in case the folder ./assets/script, then you can try to make the rules for each folder within ./assets instead of the whole folder.

  • I’ve tried everything for that /Assets at the end, but it doesn’t work at all, I tested with other folders and it works, except with the Assets folder

  • But exactly what happens, does he ignore all the files, or is he ignoring none? @Vinicius

  • Ignoring all and the one that is not to ignore it ignores too, but can leave solved otherwise, only not very productive, ignoring the folders and the files inside the script folder

  • 1

    @Very strange Vinicius work with all folders except with the folder assets/ - suggestion: include your .gitignore complete in question.

  • @Vinicius It is not because you have solved otherwise that your doubt will not serve other people, it does not cost to keep trying when you have a while, tomorrow install the git and I will test. See you tomorrow.

  • @Danielgomes also found it strange, probably he added something else that was not mentioned here.

  • I added everything that is in my . gitignore although when I put ## it turns into title

  • @Vinicius but I said to use assets/ will ignore the whole directory and what the correct one is assets/*, see in the answer how was the whole code.

  • Still not working, with no file from Assets folder

  • Look at the stack pop, @Guilhermenascimento...

  • @Vinicius edited response.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.