NewsWorld
PredictionsDigestsScorecardTimelinesArticles
NewsWorld
HomePredictionsDigestsScorecardTimelinesArticlesWorldTechnologyPoliticsBusiness
AI-powered predictive news aggregation© 2026 NewsWorld. All rights reserved.
Trending
TrumpTariffTradeAnnounceLaunchNewsPricesStrikesMajorFebruaryPhotosYourCarLotSayCourtDigestSundayTimelineSafetyGlobalMarketTechChina
TrumpTariffTradeAnnounceLaunchNewsPricesStrikesMajorFebruaryPhotosYourCarLotSayCourtDigestSundayTimelineSafetyGlobalMarketTechChina
All Articles
Don't create .gitkeep files, use .gitignore instead
Hacker News
Published 1 day ago

Don't create .gitkeep files, use .gitignore instead

Hacker News · Feb 20, 2026 · Collected from RSS

Summary

Article URL: https://adamj.eu/tech/2023/09/18/git-dont-create-gitkeep/ Comments URL: https://news.ycombinator.com/item?id=47094877 Points: 28 # Comments: 13

Full Article

2023-09-18Git only tracks files, not directories. It will only create a directory if it contains a tracked file. But sometimes you need to “track” a directory, to ensure it exists for fresh clones of a repository. For example, you might need an output directory called build.In this post, we’ll look at two ways to achieve this. First, the common but slightly flawed .gitkeep technique, then a simpler one using only a .gitignore file.The .gitkeep techniqueThis technique uses an empty file called .gitkeep:build └── .gitkeep The empty file ensures that Git creates the directory with minimal cost. Any other filename may be used, as Git doesn’t treat .gitkeep files any differently.To set this up, you might create an empty file with touch:$ touch build/.gitkeep Then ignore all files in the directory, except .gitkeep, by adding patterns in the repository’s .gitignore file:/build/* !/build/.gitkeep The first pattern ignores everything in the build directory. The second one then un-ignores the .gitkeep file, allowing it to be committed.This technique works, but it has some downsides:It requires editing two files.If the directory is renamed, .gitignore needs updating, which is easy to miss..gitkeep is not a name recognized by Git, so there’s no documentation on it, potentially confusing other developers.There’s a better way that doesn’t have these flaws.The better .gitignore techniqueThis technique uses only a short .gitignore file inside the directory:build └── .gitignore The .gitignore file has these contents:* !.gitignore The first pattern ignores all files in the directory. The second one then un-ignores the .gitignore file, so it can be committed.You can create this file with echo and file redirection:$ echo '*\n!.gitignore' > build/.gitignore When you add and commit the directory, Git will pick up on the .gitignore file first, skipping other files within the directory:$ git add build $ git status On branch main Changes to be committed: new file: build/.gitignore $ git commit -m "Track build directory" [main 1cc9120] Track build directory 1 file changed, 2 insertions(+) create mode 100644 build/.gitignore The directory is now “tracked” with a single, standard file that will work even after renames.FinDon’t ignore this technique,—Adam😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸One summary email a week, no spam, I pinky promise.Related posts:Git: Output the top-level directory of the current repositoryGit: Output just the current branch nameGit: How to add and remove execute permissionsTags: git


Share this story

Read Original at Hacker News

Related Articles

Hacker Newsabout 2 hours ago
Volatility: The volatile memory forensic extraction framework

Article URL: https://github.com/volatilityfoundation/volatility3 Comments URL: https://news.ycombinator.com/item?id=47110781 Points: 3 # Comments: 0

Hacker Newsabout 2 hours ago
Holo v0.9: A Modern Routing Stack Built in Rust

Article URL: https://github.com/holo-routing/holo/releases/tag/v0.9.0 Comments URL: https://news.ycombinator.com/item?id=47110634 Points: 4 # Comments: 1

Hacker Newsabout 2 hours ago
The Dance Floor Is Disappearing in a Sea of Phones

Article URL: https://www.bloomberg.com/news/features/2026-02-20/a-boom-in-electronic-dance-music-is-changing-club-culture Comments URL: https://news.ycombinator.com/item?id=47110549 Points: 12 # Comments: 0

Hacker Newsabout 3 hours ago
Attention Media ≠ Social Networks

Article URL: https://susam.net/attention-media-vs-social-networks.html Comments URL: https://news.ycombinator.com/item?id=47110515 Points: 63 # Comments: 13

Hacker Newsabout 3 hours ago
Minions: Stripe's one-shot, end-to-end coding agents – Stripe Dot Dev Blog

Article URL: https://stripe.dev/blog/minions-stripes-one-shot-end-to-end-coding-agents Comments URL: https://news.ycombinator.com/item?id=47110495 Points: 36 # Comments: 29

Hacker Newsabout 3 hours ago
What Is a Database Transaction?

Article URL: https://planetscale.com/blog/database-transactions Comments URL: https://news.ycombinator.com/item?id=47110473 Points: 30 # Comments: 2