From 9e7f0221b9b28107ce746d8f928a6942e8197da7 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 1 Oct 2022 11:09:29 -0500 Subject: [PATCH] Add Add A Range Of Filenames To gitignore as a Git TIL --- README.md | 3 +- git/add-a-range-of-filenames-to-gitignore.md | 34 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 git/add-a-range-of-filenames-to-gitignore.md diff --git a/README.md b/README.md index d8edc46..31436ad 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1247 TILs and counting..._ +_1248 TILs and counting..._ --- @@ -235,6 +235,7 @@ _1247 TILs and counting..._ ### Git - [Accessing a Lost Commit](git/accessing-a-lost-commit.md) +- [Add A Range Of Filename To gitignore](git/add-a-range-of-filenames-to-gitignore.md) - [Amend Author Of Previous Commit](git/amend-author-of-previous-commit.md) - [Auto-Squash Those Fixup Commits](git/auto-squash-those-fixup-commits.md) - [Caching Credentials](git/caching-credentials.md) diff --git a/git/add-a-range-of-filenames-to-gitignore.md b/git/add-a-range-of-filenames-to-gitignore.md new file mode 100644 index 0000000..6e44d84 --- /dev/null +++ b/git/add-a-range-of-filenames-to-gitignore.md @@ -0,0 +1,34 @@ +# Add A Range Of Filenames To gitignore + +The `.gitignore` file is a file where you can list files that should be ignored +by git. This will prevent them from showing up in diffs, `git status`, etc. +Most entries in the `.gitignore` file will plainly correspond to a single file. + +``` +# ignore env var files +.env +.env.local +``` + +Sometimes a project has a bunch of similarly named files. Autogenerated files +are a prime example. For instance, a web app project may contain several +sitemap files with incrementing suffix values (i.e. `sitemap-1.xml`, +`sitemap-2.xml`, `sitemap-3.xml`, ...). + +I'd like to avoid having to type those all out in my `.gitignore` file. And I +don't want to have to add new entries whenever another increment of the file is +generated. + +I can handle all the current ones and future ones in a single line using some +range pattern matching supported by the `.gitignore` file format. + +``` +# ignore sitemap files +public/sitemap-[1-99].xml +``` + +This will ignore any sitemap files suffixed with 1 to 99. I don't really expect +there to ever be more than handful of those files, so _99_ should definitely do +the trick. + +[source](https://www.golinuxcloud.com/gitignore-examples/#5_Examples_of_pattern_matching_in_gitignore)