简体   繁体   中英

gitignore: Ignore everything but specific file type in sub folder

I have a .gitignore file which doesn't seem to work properly. It doesn't upload any files in the subdirectory as intended but I added an except all .cs files, but the subdirectory remains empty. Here is my code:

/*

!.gitignore

!my_repo/build/game/Assets/Scripts/*.cs

I want nothing apart from the .cs files and the .gitignore file. but the .cs files still doesn't seem to be picked up by git. Thanks.

PS.

I tried doing this also, but it doesn't work either:

!*.cs

You need to include the directory too. See https://git-scm.com/docs/gitignore#_examples

# Ignore all (assumes ignore is in my_repo/build/.gitignore)
/*
!.gitignore    

# Exempt directories
# Also ignore all files except the sub directories in the path
!game/
game/*
!game/Assets
game/Assets/*
!game/Assets/Scripts
game/Assets/Scripts/*

# Except your CS files
!game/Assets/Scripts/*.cs

-- Update to add clarity --

It seems counter-intuitive, but you have to include the directory explicitly first because:

It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

So you have to include the directory, then ignore everything in the directory by defualt, then create your exemption.

The line !my_repo/build/game/Assets/Scripts/*.cs contains the root folder while it shoouldn't and the !*.cs looks in only in root folder.

Should be like /* !.gitignore !**/*.cs

Then add with git add -f *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM