How do I add an empty directory to a Git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Adding an empty directory to a Git repository can be a unique situation given Git’s design philosophy. Git is a distributed version control system focusing on content tracking within a repository as opposed to tracking directories or empty folders. This article will guide you through the process of adding an empty directory to a Git repository by leveraging some strategic approaches.
Understanding Git's Behavior
Git inherently does not track empty directories. This is because in Git, directories serve as a means of organizing files and nothing more. An empty directory contains no files, and since Git is a file-based version control system, it skips empty directories during the commit process. Therefore, a direct attempt to add an empty directory using the git add command will not work as expected.
Workaround: Using Dummy Files
A commonly adopted workaround for this limitation is to place a "dummy" file inside the directory. This file ensures that the directory is not empty anymore, hence allowing Git to track it. A popular choice for a dummy file is a .gitkeep file. The following steps describe how to do this:
- Create the Directory: First, create your desired directory using a shell command or file manager.
- Add a Dummy File: Inside this directory, create a
.gitkeepfile. The name is conventionally.gitkeep, but technically, it can be any filename. It doesn’t even need to contain any content.
- Stage the Changes: Now, stage the directory along with the
.gitkeepfile.
- Commit the Changes: Commit your changes to the repository.
By following these steps, the directory will be tracked by Git through the presence of the .gitkeep file.
Alternatives and Best Practices
Besides .gitkeep, there might be scenarios where another approach is preferred or required. Here are some alternatives:
- Documentation Note: Include a
README.mdinside the empty folder explaining its intended use and why it's currently empty. - Configuration Files: If applicable, a configuration file might reside within the directory, which can also serve the purpose of avoiding the directory being completely empty.
Summary Table: Key Points
| Concept | Explanation |
| Git Behavior | Git does not track empty directories as it only tracks files. |
Dummy Files (.gitkeep) | A placeholder file like .gitkeep can be used to make directories non-empty. |
| Alternative Files | Other than .gitkeep, files like README.md can also be added if it makes logical sense. |
| Stage and Commit Process | Adding and committing require staging the dummy file along with the directory path. |
Additional Considerations
- .gitkeep vs .gitignore: While
.gitkeepis often used to keep directories,.gitignoreserves a different purpose by telling Git to ignore certain files and directories. Do not confuse the two. - Cross-Platform Compatibility: Ensure any actions taken (e.g., naming, scripting for placing
.gitkeep) consider platform-specific implications, like case sensitivity. - Clean-Up: Always remember, should a directory later house real content, there's no obligation to retain the
.gitkeepunless needed for ongoing placeholders or to maintain structure robustness.
Conclusion
While Git doesn't support tracking empty directories by design, the workaround of including a dummy file, typically .gitkeep, is a widely accepted solution. It enables one to bypass this limitation efficiently, ensuring organizational structure within a repository. Understanding this aspect of Git's design philosophy and leveraging the available workarounds ensures better management and a smoother workflow when dealing with repositories that require the presence of empty structures.

