gitignore all files of extension in directory

GitGitignore

Git Problem Overview


Is there a way to ignore all files of a type in a directory?

** is apparently meaningless to git, so this doesn't work:

/public/static/**/*.js

The idea is to match arbitrary nested folders.

Git Solutions


Solution 1 - Git

It would appear that the ** syntax is supported by git as of version 1.8.2.1 according to the documentation.

> Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning: > > - A leading "**" followed by a slash means match in all directories. For > example, "**/foo" matches file or directory "foo" anywhere, the same > as pattern "foo". "**/foo/bar" matches file or directory "bar" > anywhere that is directly under directory "foo". > > - A trailing "/**" matches everything inside. For example, "abc/**" > matches all files inside directory "abc", relative to the location of > the .gitignore file, with infinite depth. > > - A slash followed by two consecutive asterisks then a slash matches > zero or more directories. For example, "a/**/b" matches "a/b", > "a/x/b", "a/x/y/b" and so on. > > - Other consecutive asterisks are considered invalid.

Solution 2 - Git

Never tried it, but git help ignore suggests that if you put a .gitignore with *.js in /public/static, it will do what you want.

Note: make sure to also check out Joeys' answer below: if you want to ignore files in a specific subdirectory, then a local .gitignore is the right solution (locality is good). However if you need the same pattern to apply to your whole repo, then the ** solution is better.

Solution 3 - Git

UPDATE: Take a look at @Joey's answer: Git now supports the ** syntax in patterns. Both approaches should work fine.


The gitignore(5) man page states:

> Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file.

What this means is that the patterns in a .gitignore file in any given directory of your repo will affect that directory and all subdirectories.

The pattern you provided

/public/static/**/*.js

isn't quite right, firstly because (as you correctly noted) the ** syntax is not used by Git. Also, the leading / anchors that pattern to the start of the pathname. (So, /public/static/*.js will match /public/static/foo.js but not /public/static/foo/bar.js.) Removing the leading / won't work either, matching paths like public/static/foo.js and foo/public/static/bar.js. EDIT: Just removing the leading slash won't work either — because the pattern still contains a slash, it is treated by Git as a plain, non-recursive shell glob (thanks @Joey Hoer for pointing this out).

As @ptyx suggested, what you need to do is create the file <repo>/public/static/.gitignore and include just this pattern:

*.js

There is no leading /, so it will match at any part of the path, and that pattern will only ever be applied to files in the /public/static directory and its subdirectories.

Solution 4 - Git

I have tried opening the .gitignore file in my vscode, windows 10. There you can see, some previously added ignore files (if any).

To create a new rule to ignore a file with (.js) extension, append the extension of the file like this:

*.js

This will ignore all .js files in your git repository.

To exclude certain type of file from a particular directory, you can add this:

**/foo/*.js

This will ignore all .js files inside only /foo/ directory.

For a detailed learning you can visit: https://www.atlassian.com/git/tutorials/saving-changes/gitignore">about git-ignore

Solution 5 - Git

To ignore untracked files just go to .git/info/exclude. Exclude is a file with a list of ignored extensions or files.

Solution 6 - Git

I believe the simplest solution would be to use find. I do not like to have multiple .gitignore hanging around in sub-directories and I prefer to manage a unique, top-level .gitignore. To do so you could simply append the found files to your .gitignore. Supposing that /public/static/ is your project/git home I would use something like:

find . -type f -name *.js | cut -c 3- >> .gitignore

I found that cutting out the ./ at the beginning is often necessary for git to understand which files to avoid. Therefore the cut -c 3-.

Solution 7 - Git

Few additional tips on top of other answers (which may work if you're lucky and issue is due to some other reason):

  1. Make sure u ignore files larger than 100mb in your code
  2. Just restart the git workflow. Delete the local .git folder and git init again and retry pushing to github.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarryView Question on Stackoverflow
Solution 1 - GitjoeyhoerView Answer on Stackoverflow
Solution 2 - GitptyxView Answer on Stackoverflow
Solution 3 - GitAdam SharpView Answer on Stackoverflow
Solution 4 - GitTuhin MitraView Answer on Stackoverflow
Solution 5 - GitThiago RamosView Answer on Stackoverflow
Solution 6 - GitGariniView Answer on Stackoverflow
Solution 7 - GitBaraja SwargiaryView Answer on Stackoverflow