Ignore everything but one file in .gitignore

Programming

14 Apr 2020 | 1 minute read

Sometimes it's useful to ignore everything but one or two files in .gitignore.

For example, when using GatsbyJS and Netlify, redirects are placed in the public folder as _redirects. As the public folder also is used when building your Gatsby site, it's a good practice not to include it in version control. But then, how do we include the _redirects and nothing else?

All you need to do is to prepend ! to the file you don't want to have ignored in your .gitignore.

For our use-case above with the Netlify _redirects file, our .gitignore would look like this:

public/*
!public/_redirects