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 filesuploads/: Files uploaded by userstmp/: Temporary filescache/: 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?
- Create a file named
.gitkeepinside the empty folder. - The file content can be empty, or you can write “Keep this directory”.
git addandcommitthis 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
.gitkeepis 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
.gitignoreworks;.gitkeepis just a little tool to help Git “make sense” of empty folders.