logoalt Hacker News

Don't create .gitkeep files, use .gitignore instead (2023)

71 pointsby frou_dhyesterday at 10:27 PM41 commentsview on HN

Comments

jkubicektoday at 2:12 AM

I'm not sure if I'm the one to blame for this or not, but the earliest reference to ".gitkeep" I can find online is my 2010 answer on Stack Overflow: https://stackoverflow.com/a/4250082/28422

If this is all my fault, I'm sorry.

show 1 reply
Arrowmastertoday at 1:46 AM

The author makes a very common mistake of not reading the very first line of the documentation for .gitignore.

  A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.
You should never be putting "!.gitignore" in .gitignore. Just do `echo "*" > .gitignore; git add -f .gitignore`. Once a file is tracked any changes to it will be tracked without needing to use --force with git add.
show 5 replies
GreenDolphinSystoday at 4:21 AM

.gitkeep is intuitive and easy to understand. Unignoring a .gitignore is not intuitive. This falls squarely into "clever optimization tricks that obscure intent and readability". Don't do things like this.

It's not that hard to update a .gitignore file every now and then.

beej71today at 4:04 AM

What am I missing about this use case? It seems like you should just create `build/.gitignore` with `*` in it and `add -f` it and be done.

I'd use `.gitkeep` (or an empty `.gitignore`) if I needed to commit an otherwise-empty hierarchy. But if I'm going to have a `.gitignore` in there anyway, it's not empty.

> The directory is now “tracked” with a single, standard file that will work even after renames.

Does `.gitkeep` not work after renames? Or `.gitignore`?

So I am missing something. :)

show 1 reply
cortesofttoday at 1:02 AM

Not sure why you can’t just have your build script create the build directory?

show 1 reply
Kurajtoday at 3:18 AM

If you need to do this, I think .gitkeep communicates intent better. You don't need to document it or risk it being removed as thought to be a left over.

dmarinustoday at 6:53 AM

if possible you can also just create directories if they don't exist (ie. mkdir -p) and just exclude it in your root .gitignore (ie. ignore all build directories). That would safe you from creating multiple .gitignore files.

8cvor6j844qw_d6today at 1:59 AM

Is .gitkeep an established convention somewhere? I'm curious where the name originated.

show 1 reply
OptionOfTtoday at 3:48 AM

For me, I put them in directories that have to be there, because the underlying code doesn't create the directory, and without it, it fails.

Another example is where you want an empty directory mounted in Docker. If the directory is not there it is created with root permissions and then I can't even look into it.

yjftsjthsd-htoday at 12:52 AM

I'm confused. Having a file gitignored doesn't stop you from committing it; AFAIK you can just

  touch build/.gitkeep
  git add build/.gitkeep
  git commit build/.gitkeep
And that's it? There's no need to exclude anything.
show 1 reply
kderbymatoday at 3:37 AM

Arent Gitkeep files specifically for empty folders that are intended to be there?

That is what I have always used them for....

suralindtoday at 1:06 AM

I want to like it, but I pretty much always have a "cleanup" script that just deletes the entire directory and touches a .gitkeep file. Obviously an even better pattern is to not have any .gitkeep files, but sometimes they are just handy.

macotetoday at 1:07 AM

The author is misusing .gitkeep. I use it to keep source code folders that don’t contain any code yet, but whose structure is already defined.

show 2 replies
cyberrocktoday at 4:53 AM

File filtering is so delightfully broken everywhere. Everytime I revisit git, rsync, restic, borg, etc. something just goes wrong somewhere on this seemingly simple task, and SO and thus LLMs are filled to the brim with slightly wrong answers. We need a xkcd/927 because it can't possibly get any worse.

peter-m80today at 1:21 AM

No, thanks