How to whitelist files in .gitignore
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 sarunw.com by checking out this sponsor.
Localization Buddy: Easiest way to localize and update App Store metadata.
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.
.env
.env.prod
.env.staging
.env.template
- 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
.env
// Ignore .env.staging, .env.prod, and .env.template
.env.*
// Re-include .env.template
!.env.template
Caveat
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.
logs
- 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
logs
// Re-include important.txt won't work
// Because a parent directory is already ignored.
!important.txt
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
logs/*
// Re-include important.txt
!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 https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareHow 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.