How to use .gitkeep in Git

Using .gitkeep files in Git allows you to track empty directories that are essential for project structure, build processes, or deployment workflows. As the creator of CoreUI with 25 years of development experience since 2000, I’ve used .gitkeep files in numerous projects to maintain critical directory structures for build outputs, logs, and deployment assets. The most effective approach involves creating .gitkeep files in empty directories that need to exist in the repository for proper application functionality. This method ensures consistent project structure across development environments while working within Git’s file-based tracking limitations.

Create .gitkeep files in empty directories to ensure they are tracked and maintained in Git repositories.

# Create project directories that need to exist
mkdir -p logs
mkdir -p uploads/temp
mkdir -p build/assets
mkdir -p cache/sessions

# Add .gitkeep files to track empty directories
touch logs/.gitkeep
touch uploads/.gitkeep
touch uploads/temp/.gitkeep
touch build/.gitkeep
touch build/assets/.gitkeep
touch cache/.gitkeep
touch cache/sessions/.gitkeep

# Add .gitkeep files to Git
git add logs/.gitkeep
git add uploads/.gitkeep
git add uploads/temp/.gitkeep
git add build/.gitkeep
git add build/assets/.gitkeep
git add cache/.gitkeep
git add cache/sessions/.gitkeep

# Commit the directory structure
git commit -m "Add .gitkeep files to maintain directory structure"

# Verify directories are tracked
git ls-files | grep .gitkeep

The .gitkeep file is a simple placeholder file that allows Git to track directories that would otherwise be ignored because Git only tracks files, not empty directories. These files can be empty or contain comments explaining the directory’s purpose. When other developers clone the repository, the directory structure will be preserved, ensuring that applications don’t fail due to missing folders that the code expects to exist for logging, uploads, or build processes.

Best Practice Note:

This is the directory management approach we use in CoreUI project templates to maintain consistent structure across development teams. Consider adding comments inside .gitkeep files explaining the directory purpose and ensure your .gitignore doesn’t accidentally exclude the contents these directories will contain.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
Javascript Random - How to Generate a Random Number in JavaScript?
Javascript Random - How to Generate a Random Number in JavaScript?

What is Double Question Mark in JavaScript?
What is Double Question Mark in JavaScript?

How to Manage Date and Time in Specific Timezones Using JavaScript
How to Manage Date and Time in Specific Timezones Using JavaScript

How to sleep in Javascript
How to sleep in Javascript

Answers by CoreUI Core Team