repository
git
directory

How do I add an empty directory to a Git repository?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Git does not track empty directories because Git tracks files rather than directories. However, there are some common workarounds to ensure that an empty directory is included in your Git repository.

1. Use a .gitkeep File

The most common practice is to add a hidden file, usually named .gitkeep, to the empty directory. This file has no special meaning to Git, but it allows the directory to be tracked because it is no longer empty.

Steps:

  1. Create the directory if it doesn't exist:
bash
 mkdir path/to/your/empty-directory
  1. Create a .gitkeep file in the empty directory:
bash
 touch path/to/your/empty-directory/.gitkeep
  1. Add the directory and file to Git:
bash
 git add path/to/your/empty-directory/.gitkeep
  1. Commit the change:
bash
 git commit -m "Add empty directory with .gitkeep"

2. Use an Empty .gitignore File

Another approach is to add an empty .gitignore file inside the empty directory. This method might be useful if you want to include a directory but prevent any other files from being accidentally committed to it.

Steps:

  1. Create the directory if it doesn't exist:
bash
 mkdir path/to/your/empty-directory
  1. Create an empty .gitignore file in the empty directory:
bash
 touch path/to/your/empty-directory/.gitignore
  1. Add the directory and file to Git:
bash
 git add path/to/your/empty-directory/.gitignore
  1. Commit the change:
bash
 git commit -m "Add empty directory with .gitignore"

3. Comment in the .gitignore File

If you want to use a .gitignore file but ensure that it has content (because empty files might be removed by some editors), you can add a comment inside the .gitignore file:

Steps:

  1. Create the directory if it doesn't exist:
bash
 mkdir path/to/your/empty-directory
  1. Create a .gitignore file with a comment:
bash
 echo "# This directory is intentionally left empty" > path/to/your/empty-directory/.gitignore
  1. Add the directory and file to Git:
bash
 git add path/to/your/empty-directory/.gitignore
  1. Commit the change:
bash
 git commit -m "Add empty directory with .gitignore"

Summary

  • Use .gitkeep: This is a conventional way to include an empty directory by placing a .gitkeep file inside.
  • Use .gitignore: If you prefer, you can place an empty .gitignore file or one with a comment inside the directory.

These methods allow you to ensure that the directory is tracked by Git even if it contains no files.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.