162

Like the title says, is it possible to add "files without dots in them" to the gitignore file?

I imagine this would take care of all those bothersome extensionless files.

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 5
    There are some extensionless files that you don't want to ignore: `README`, `LICENSE` or `COPYING`, `INSTALL`, `Makefile` etc., though you can always force-add them and then they are tracked – Jakub Narębski Sep 26 '13 at 09:14
  • @JakubNarębski good point. I have included it in my answer for more visibility. – VonC Sep 26 '13 at 09:34
  • possible duplicate of [gitignore without binary files](http://stackoverflow.com/questions/5711120/gitignore-without-binary-files) – Mad Physicist Jul 09 '15 at 19:20

3 Answers3

188

You can try a combination similar to:

*
!/**/
!*.*

That gitignore exclusion rule (a negated pattern) should ignore all files, except the ones with an extension.

As mentioned below by Mad Physicist, the rule is:

It is not possible to re-include a file if a parent directory of that file is excluded. (*)
(*: unless certain conditions are met in git 2.?+, see below)

That is why !/**/ is important (white-listing the parent folders recursively) if we want to white-list files.

I mentioned that same rule in similar cases like:


As Jakub Narębski comments, you might not want to ignore all extensionless files.

My advice:

  • add first the extensionless file that matters
  • then edit your .gitignore as shown above: the already versioned files won't be ignored (even if they don't have an extension). All the others will be ignored.

For any future extensionless files that you would want to version:

git add -f -- myFile

Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

However, since one of the rules to re-inclusion was:

The directory part in the re-include rules must be literal (i.e. no wildcards)

This wouldn't have worked here anyway.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    The answer is completely correct. But perhaps a better `.gitignore` would be `**` and `!**.*`. I know this doesn't make a difference if the asterisk is placed in front, but it is more explicit and sound with how shells handle wildcards... – Willem Van Onsem Jan 10 '15 at 04:58
  • 8
    Note: to work with other exclusion rules, this will need to be the first set of rules in your .gitignore file – Erik Jan 12 '15 at 22:41
  • You can also reinclude certain extensionless files e.g. with `!Makefile`. Beware that I experienced problems with this solution in Git 1.7.1 while it worked fine in 1.9.0. In my case adding a file in the root directory worked fine, but adding a file with an extension inside a subdirectory was shown as ignored. This seems to be because 1.7.1 didn't support the `**` pattern yet, see [here](https://git-scm.com/docs/gitignore/1.7.1)! – mxmlnkn Mar 07 '18 at 19:57
  • 3
    `**` was added in Git `1.8.2`. Is there any disadvantage over using `!*/` instead of `!/**/`? – mxmlnkn Mar 07 '18 at 20:03
  • @TamaMcGlinn That looks like a regex: gitignore does not support regexes. – VonC Oct 03 '18 at 15:42
  • The re-include solution will affect `ag` which also read the `.gitignore` file but doesn't support re-include. waiting for a better solution. – luochen1990 May 14 '19 at 07:08
  • This is good, but you cannot ignore any files with extension. – Abdullah Nurum Apr 06 '22 at 18:51
77
*
!*/
!*.*

* tells git to ignore everything.

!*/ then unignores anything that is a directory. This is crucial.

!*.* unignores all files with an extension.

Without the !*/ rule, directories without a . in the name would not be listed and none of your desired files would be added outside the root folder.

For reference, read these two sections in the .gitignore documentation stand out:

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. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 2
    Good catch. +1. I had forgotten to update my answer with that rule I had recently used in other answers of mines. I have updated my answer accordingly. – VonC Jul 09 '15 at 20:06
5

In my folders there are lots of files with *.c, *.h, *.txt, *.csv etc. extensions and binary files without any extension. So I needed to ignore all files execpt *.c,*.h and .gitignore, So this works for me, from the .gitignore example:

 */*         #ignore all files in each directory
 !*/*.c      #unignore .c files in each directory
 !*/*.h      #unignore .h header files in each directory
 !.gitignore #unignore .gitignore
Ando Avakian
  • 61
  • 1
  • 4