不要创建 .gitkeep 文件,使用 .gitignore 代替。
Don't create .gitkeep files, use .gitignore instead (2023)

原始链接: https://adamj.eu/tech/2023/09/18/git-dont-create-gitkeep/

## 使用 Git 跟踪空目录 Git 本身不跟踪空目录,但有时你需要确保一个目录存在于仓库中(例如 `build` 文件夹)。传统上,开发者使用 `.gitkeep` 文件——一个放置在目录中的空文件——以及特定的 `.gitignore` 规则来强制 Git 识别它。然而,这种方法需要编辑两个文件,如果目录被重命名,则容易出错。 一个更简单、更可靠的解决方案是*仅*在你要跟踪的目录**内部**放置一个 `.gitignore` 文件。这个 `.gitignore` 文件包含两行:`*`(忽略所有内容)和 `!.gitignore`(取消忽略 `.gitignore` 文件本身)。 这种方法确保目录使用单个标准文件进行跟踪,自动适应重命名,并避免了非标准 `.gitkeep` 文件的混淆。它是一种更简洁、更易于维护的方式来管理 Git 仓库中的空目录。

Hacker News新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交登录 不要创建 .gitkeep 文件,使用 .gitignore 代替 (adamj.eu) 13 分,来自 frou_dh 2 小时前 | 隐藏 | 过去 | 收藏 | 3 条评论 帮助 cortesoft 2 分钟前 | 下一个 [–] 不确定为什么你的构建脚本不能创建构建目录?回复 yjftsjthsd-h 11 分钟前 | 上一个 [–] 我感到困惑。被 git 忽略的文件并不能阻止你提交它;据我所知,你可以这样做: touch build/.gitkeep git add build/.gitkeep git commit build/.gitkeep 这样就可以了?不需要排除任何东西。回复 williadc 3 分钟前 | 父级 [–] 想法是你不希望签入任何构建。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
Crystal a or simpler crystal b?

Git 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 technique

This technique uses an empty file called .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:

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:

  1. It requires editing two files.
  2. If the directory is renamed, .gitignore needs updating, which is easy to miss.
  3. .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 technique

This technique uses only a short .gitignore file inside the directory:

build
└── .gitignore

The .gitignore file has these contents:

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.

Fin

Don’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:

Tags:

© 2023 All rights reserved. Code samples are public domain unless otherwise noted.

联系我们 contact @ memedata.com