Don't create .gitkeep files, use .gitignore instead (2023)

Reconsidering .gitkeep: A Better Approach to Git Repository Management

As developers, we're always looking for ways to optimize our workflow and make our codebases more efficient. One common practice that's been debated in the developer community is the use of .gitkeep files. In a recent article, Adam Johnson makes a compelling case for ditching .gitkeep files in favor of a more effective approach: using .gitignore instead.

Why .gitkeep Files Are Problematic

If you're not familiar with .gitkeep files, they're empty files created in empty directories to force Git to track those directories. The idea behind this is to ensure that empty directories are included in the repository, which can be useful for various reasons, such as maintaining a specific directory structure or including empty directories that will be populated later.

However, as Adam Johnson points out, using .gitkeep files can lead to unnecessary clutter in your repository. These files can accumulate over time, making it harder to navigate your codebase and understand the purpose of each directory. Moreover, .gitkeep files don't provide any meaningful information about the directories they're inhabiting, which can lead to confusion and mismanagement of your repository.

How to Use .gitignore Effectively

So, what's the alternative? Johnson suggests using .gitignore files to achieve the same goal, but with more flexibility and control. By adding a .gitignore file to an empty directory, you can specify which files or directories should be ignored by Git. This approach allows you to maintain a clean repository while still ensuring that empty directories are tracked.

For example, you can add the following line to your .gitignore file to ignore all files in a directory except for a specific subdirectory:

# Ignore all files in the 'docs' directory except for the 'images' subdirectory
docs/*
!docs/images

This way, you can keep your repository organized and focused on the code that matters, without the need for unnecessary .gitkeep files.

Benefits of Using .gitignore

Using .gitignore files instead of .gitkeep files has several benefits:

  • Less clutter: By avoiding the creation of empty .gitkeep files, you can keep your repository tidy and easier to navigate.
  • More control: .gitignore files provide more fine-grained control over which files and directories are tracked by Git.
  • Improved readability: By specifying which files and directories to ignore, you can make your repository more self-explanatory and easier to understand.

Who is this for?

This approach is suitable for any developer or team looking to optimize their Git workflow and maintain a clean, organized repository. Whether you're working on a small project or a large-scale enterprise application, using .gitignore files instead of .gitkeep files can help you streamline your development process and reduce unnecessary complexity.

So, the next time you're tempted to create a .gitkeep file, consider using a .gitignore file instead. Your repository (and your fellow developers) will thank you.

What's your take on using .gitkeep files versus .gitignore files? Do you have any favorite Git tips or tricks to share?

Read more

🚀 Global, automated cloud infrastructure

Oracle Cloud is hard to get. I recommend Vultr for instant setup.

Get $100 in free server credit on Vultr →