How to whitelist files in .gitignore

⋅ 1 min read ⋅ Development

Table of Contents

.gitignore is a file that specifies any files and folders that you don't want to commit to git.

But you might not be aware of the ability to re-include an ignored file with the ! (exclamation mark) operator.

You can easily support by checking out this sponsor.

Sponsor and reach thousands of iOS developers.

How to whitelist files in .gitignore

To whitelist a file that is currently ignored:

  • You declare another pattern that points to the file you want to whitelist.
  • Prefix the pattern with !.

For example, my project contains the following .env files.

  • I want to ignore those .env files because they contain sensitive information.
  • But I want to include .env.template because it contains placeholders for all my keys. I want to share this with my teammates.

This is the .gitignore that does that.

// Ignore .env
// Ignore .env.staging,, and .env.template
// Re-include .env.template


It is not possible to re-include a file if a parent directory of that file is ignored.

For example, a project contains the following files and folders.

- log1.txt
- log2.txt
- important.txt

If I want to ignore logs, but include only important.txt, this .gitignore won't work.

// Ignore logs directory
// Re-include important.txt won't work
// Because a parent directory is already ignored.

To make this work, I need to leave the logs directory out and ignore everything inside the logs instead.

// Ignore everything inside the logs directory
// Re-include important.txt

Read more article about Development or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
How to Deprecate old API in Swift

In Swift, we have many options to mark a method deprecated. Let's explore all of them.

How to add Launch Screen in SwiftUI

If you create a new SwiftUI project, you won't see a launch screen storyboard anymore. Learn how to configure a launch screen in the SwiftUI world.

← Home