Featured image of post Why Doesn't Git Track Empty Folders? What Are .gitkeep and .gitignore?

Why Doesn't Git Track Empty Folders? What Are .gitkeep and .gitignore?

Deep dive into why Git ignores empty folders, and how to properly use .gitkeep and .gitignore to manage project directory structure. Includes practical examples and best practices.

Simply put, .gitkeep is a conventional dummy file. Its existence serves only one purpose: to force Git to track a folder that should otherwise be empty.

Why Is It Needed?

Git’s design logic is to “track file changes”, not “track folders”. If your project contains an empty folder, Git will ignore it completely and will not upload it to GitHub or your server.

This creates a problem: sometimes your code requires a certain directory to exist to run, but you don’t want to upload test data or temporary files. If the directory doesn’t exist, the program might crash.

These Folders Are Usually:

  • logs/: For log files
  • uploads/: Files uploaded by users
  • tmp/: Temporary files
  • cache/: Cache data

These directories are needed at runtime, their contents shouldn’t be committed, but the folder itself must exist.

How It Works

To “trick” Git into keeping this folder, we place a hidden file with no real function inside it, making Git think, “Oh! There’s something here, I need to record it.”

Practical Analogy:

Imagine you are building a house (writing a project):

File Description
.gitignore Like a “Trash Tag”, telling the mover (Git) what items (like trash, scraps) absolutely should not be moved into the new house.
.gitkeep Like a “Placeholder Box”. The new house isn’t furnished yet, but you need to reserve space for the storage room, so you put a box there to let the movers know this area is part of the house and shouldn’t be demolished.

How to Use It?

  1. Create a file named .gitkeep inside the empty folder.
  2. The file content can be empty, or you can write “Keep this directory”.
  3. git add and commit this file.

Comparing .gitkeep vs .gitignore

Feature .gitkeep .gitignore
Official Status Unofficial (Just a community convention) Official (Built-in Git feature)
Main Function Keep a folder, prevent it from being forgotten by Git Exclude files, prevent them from being tracked by Git
Common Scenarios Empty log folders, cache folders Password files, node_modules, temporary files

Practical Advice: Thinking Outside the Box

Although .gitkeep is commonly used, it’s actually not part of Git officially. In fact, you could use README.md or .placeholder to achieve the same effect. But why does everyone refer to .gitkeep? Because it’s self-explanatory: “This file does nothing but keep Git from deleting this folder.”

If you want to look a bit more professional, there is a more creative approach that is more functional than just using .gitkeep:

Place a .gitignore in the empty folder with the following content:

# Ignore everything in this directory
*
# But do not ignore me (this .gitignore file itself)
!.gitignore

This ensures that you keep the folder while ensuring that junk files inside are never accidentally committed. This is the true “foolproof” method.

Conclusion

  • .gitkeep is a human compromise to Git’s design logic.
  • It is not an official specification, but has become a de facto standard for developers.
  • What’s truly important is understanding how .gitignore works; .gitkeep is just a little tool to help Git “make sense” of empty folders.

Reference

All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy