68

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
miken32
  • 42,008
  • 16
  • 111
  • 154
javier_domenech
  • 5,995
  • 6
  • 37
  • 59

4 Answers4

62

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

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • thanks, it works. But could it be possible to not ignore any folder even if we don't specify it in .gitignore? – javier_domenech Jul 10 '14 at 14:15
  • Thanks a lot for your explanation, as far as I understand now, gitignore rules doesn't difference between folders and files. If we had a way to tell "this rule is for files" or "this rule is for dirs" .. – javier_domenech Jul 10 '14 at 14:32
  • 3
    @vivoconunxino no thats not possible; git doesn't track folders at all. The entries in gitignore are patterns, and any filename or filepath(means directory) matching it is ignored. – Anshul Goyal Jul 10 '14 at 14:34
  • Is there some way to write the exception before the ignore statement and still make it function properly? – Aaron John Sabu Apr 25 '20 at 23:32
39

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
kalnar
  • 686
  • 7
  • 9
17

This works for me (excludes all imgs folder content except .gitkeep files)

/imgs/**/*.*
!/imgs/**/.gitkeep
mazz
  • 423
  • 1
  • 4
  • 11
  • exactly what I needed. (I didn't need the beginning slash) – takanuva15 Jun 30 '20 at 19:16
  • 1
    Note that this won't work if there are `.` characters in your directory names. You'll have to insert the following between your two lines `!/imgs/**/*.*/` – mbaker3 Aug 24 '22 at 20:27
6

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.

Roald
  • 2,459
  • 16
  • 43