Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add files without dots in them (all extension-less files) to the gitignore file?

Tags:

git

gitignore

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.

like image 835
The Unfun Cat Avatar asked Sep 26 '13 08:09

The Unfun Cat


People also ask

How do I ignore a file without adding to Gitignore?

To ignore untracked files, you have a file in your git folder called . git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.

How do I use wildcards in Gitignore?

. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

Can I add multiple Gitignore files?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .


2 Answers

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:

  • Gitignore all except one folder and all its content - regardless of the nesting level
  • Git except a sub directory and it's files of a ignored directory
  • Gitignore exclude certain files in all subdirectories

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:

  • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
  • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

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.

like image 155
VonC Avatar answered Sep 27 '22 20:09

VonC


* !*/ !*.* 

* 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).

like image 32
Mad Physicist Avatar answered Sep 27 '22 18:09

Mad Physicist