Reputation: 6263
What I want is to ignore all kind of files except the .php ones, but with this .gitignore I'm also ignoring folders...
#ignore all kind of files
*
#except php files
!*.php
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
Upvotes: 75
Views: 42845
Reputation: 21
You need re-include every dir in path to your file:
# ignore all kind of files
*
# except all dirs on the way
!**/
# except php files
!*.php
If you have different files split between different dirs:
*
# except php dir
!/php/
# except every directory in php
!/php/**/
# except every php file in php dir
!/php/**/*.php
# and same for css
!/css/
!/css/**/
!/css/**/*.css
Upvotes: 2
Reputation: 842
Couldn't find the one that I was looking for, so adding the one that worked for me.
The following declaration states that,
Now if you note step-3
, you can see that it has cumulative effect of step-1, 2 declarations
. ie, when unignoring each subdir recursively, ignore everything directly under that subdir except few files ignored in step-2. This will be carried out for each subdir that is being processed
, hence the order/sequence of declaration is important.
Example:
subdir1
\subdir11\others.txt
\subdir12\build.xml
.gitignore
subdir2
\subdir21\resources\build.xml
\subdir22\build.xml
build.xml
.gitignore
.gitignore
file should contain
# !! NOTE: the sequence of declaration is important !!
# Ignore Everything
*
# Exclude specific files from being ignored (override)
!.gitignore
!*.xml
# overrides stated above, applies to all dir (root and subdirs - recursively)
!**/
Results:
subdir1\subdir12\build.xml
subdir1\.gitignore
subdir2\subdir21\resources\build.xml
subdir2\subdir22\build.xml
build.xml
.gitignore
After this if you still want to trim down or filter more
out of the ones you have filtered already, add it next to the processing
(remember: Sequence is important)
# !! NOTE: the sequence of declaration is important !!
# Ignore Everything
*
# Exclude specific files from being ignored (override)
!.gitignore
!build.xml
# overrides stated above, applies to all dir (root and subdirs - recursively)
!**/
**/resources/**
Now This will add step-4 which is to ignore *.xml (in subdirs only) whose subdir path contains intermediate resources
dir, that were filtered as results till step-3.
Results:
subdir1\subdir12\build.xml
subdir1\.gitignore
subdir2\subdir22\build.xml
build.xml
.gitignore
This doesn't stop here, you can also ignore unignored files in step-2 only for subdirs
(So that they are included only for rootdir but not in subdirs)
# !! NOTE: the sequence of declaration is important !!
# Ignore Everything
*
# Exclude specific files from being ignored (override)
!.gitignore
!build.xml
# overrides stated above, applies to all dir (root and subdirs - recursively)
!**/
**/resources
**/.gitignore
In addition to step-4 results this will exclude .gitignore
in all subdirs except for root dir.
Upvotes: 0
Reputation: 2979
Note that if the !
has no effect, you probably excluded a folder. From the docs (emphasis mine):
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. 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.
Upvotes: 7
Reputation: 433
This works for me (excludes all imgs folder content except .gitkeep files)
/imgs/**/*.*
!/imgs/**/.gitkeep
Upvotes: 18
Reputation: 726
I had a similar problem; I wanted to whitelist *.c
, but the accepted answer didn't work for me, because I had files that didn't contain ".".
So for those who want to handle that:
# ignore everything
*
# but don't ignore files ending with slash = directories
!*/
# and don't ignore files ending with ".php"
!*.php
Upvotes: 42
Reputation: 76887
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
Upvotes: 68