Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

gitignore - git ignore all files except one extension and folder structure

This is my .gitignore:

#ignore all kind of files
*
#except php files
!*.php

All I want is to ignore all kind of files except the .php ones, but with this .gitignore I'm also ignoring folders...

Is there a way to tell git to accept my project folder structure while keeping the track only of the .php files?

It seems like now I can't add folders to my repo:

vivo@vivoPC:~/workspace/motor$ git add my_folder/
The following paths are ignored by one of your .gitignore files:
my_folder
Use -f if you really want to add them.
fatal: no files added
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is simple, just add another entry !my_folder in your .gitignore

#ignore all kind of files
*
#except php files
!*.php
!my_folder

The last line will take special care of my_folder, and will not ignore any php files within it; but files within other folders will still be ignored because of the first pattern of *.

EDIT

I think I misread your question. If you want to ignore all files except .php files, you can use

#ignore all kind of files
*.*
#except php files
!*.php

This will not ignore any file which doesn't have an extension (example: if you have README and not README.txt ), and will ignore any folder with a . in its name (example: directory named module.1).

FWIW, git doesn't track directories, and hence there is no way to specify ignore rules for directory vs file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...