How to add a directory but ignore included files in git (#snippet)

This post is a quick reference for my future self. I implemented the eleventy cache assets plugin to speed up my local development last weekend. This site is based on API data from several sources, and unfortunately, local developme…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

This post is a quick reference for my future self. I implemented the eleventy cache assets plugin to speed up my local development last weekend. This site is based on API data from several sources, and unfortunately, local development became terribly slow. It was time for some optimization. The problem was that the local build process fetched all the API data on every file change and local rebuild.

The eleventy cache plugin helps here. It caches data across builds and writes the API data into files in a .cache directory. When the site rebuilds, the cached data is reused, and no APIs will be called during the build process.

To make the plugin work in a remote environment such as Netlify, it needs the .cache directory to be available. This behavior led to the problem that I had to check in the .cache directory but needed to ignore all the included files.

I ran into this exact problem many times, and it always takes me several attempts to get it right. Here's the solution that worked for me.

First, add the directory to your local project and place a .gitignore file in it.

.cache
  |_ .gitignore

Then, paste the following configuration into the .gitignore file in the directory you want to check in.

# File: .cache/.gitignore
# Ignore everything in this directory
*
# Except for this file
!.gitignore

And voilà! Commit the .gitignore file at folder-you-want-to-check-in/.gitignore, and that's it! The directory is now available in git and files in this directory will be ignored.

It's to point out that .gitignore files work in sub-directories and allow you to configure git on a sub-directory level. And that's pretty handy!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-05-30T22:00:00+00:00) How to add a directory but ignore included files in git (#snippet). Retrieved from https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/

MLA
" » How to add a directory but ignore included files in git (#snippet)." Stefan Judis | Sciencx - Sunday May 30, 2021, https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/
HARVARD
Stefan Judis | Sciencx Sunday May 30, 2021 » How to add a directory but ignore included files in git (#snippet)., viewed ,<https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to add a directory but ignore included files in git (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/
CHICAGO
" » How to add a directory but ignore included files in git (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/
IEEE
" » How to add a directory but ignore included files in git (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/. [Accessed: ]
rf:citation
» How to add a directory but ignore included files in git (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2021/05/30/how-to-add-a-directory-but-ignore-included-files-in-git-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.