Jason
Jason

Reputation: 2915

Shorthand for multiple file extensions in.gitignore

Is there a Bash script-like shorthand for matching multiple different extensions in a .gitignore file such as:

*.{bak,save,aux,nav,toc}

?

The syntax above definitely does not work.

Upvotes: 24

Views: 10310

Answers (2)

Mogzol
Mogzol

Reputation: 1405

The method mentioned in the other answer, *[.bak, .save, .aux,. nav, .toc], does not work. Or at least, not as expected. It actually selects much more than just those extensions.

As far as I can tell, there is no way to do this, unless the extensions you want to ignore are all a single character long.

According to Git's documentation:

"[]" matches one character in a selected range

As can be seen in the example later on that page:

# ignore objects and archives, anywhere in the tree.
*.[oa]

This would ignore any .o or .a files.

So this means that a pattern like *[.ext, .txt, .bin] will behave identically to *[beintx., ] (which is just the same pattern with duplicate characters removed and sorted) and will ignore any file ending in (including extension) any one of those nine characters.

I just tried this out on a Git repository of my own and it does indeed seem to follow this behavior.

Upvotes: 24

Lana White
Lana White

Reputation: 131

Couldn't find answers online, so tried it out myself and this worked (creating a test local repository with SourceTree). This is editing the .gitignore file.

*[.bak, .save, .aux,. nav, .toc]

spaces or no spaces in between the comma and full stop are ok too.

So in my case I had to do this

Ignore all files in this directory, except for dll and dic files:
Web.Site/Third-party Assemblies/*
!Third-party Assemblies/**/*[.dll, .dic]

Upvotes: 1

Related Questions