Test Empty Directories
In this step, we will explore how Git handles empty directories. This is a common point of confusion for beginners, as Git behaves differently with directories compared to files.
First, make sure you are in your ~/project/my-time-machine
directory:
cd ~/project/my-time-machine
Now, let's create a new, empty directory inside our project:
mkdir empty-folder
We have created the directory. Now, let's check the status of our repository using git status
:
git status
You should see output similar to this:
On branch master
nothing to commit, working tree clean
Notice that Git does not report empty-folder
as an untracked directory. This is because Git tracks file content, not directories themselves. An empty directory has no content to track.
This is an important concept in Git. If you need to include an empty directory in your repository, the common workaround is to place a dummy file inside it. A common practice is to create a file named .gitkeep
(though the name doesn't matter, it's just a convention).
Let's create a .gitkeep
file inside empty-folder
:
touch empty-folder/.gitkeep
Now, let's check the git status
again:
git status
This time, you should see:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
untracked files present (use "git add" to track)
Untracked files:
(use "git add <file>..." to include in what will be committed)
empty-folder/
Now Git sees the empty-folder/
because it contains a file (.gitkeep
) that can be tracked.
This demonstrates that Git tracks the presence of files within directories, rather than the directories themselves. To include a directory in your repository's history, it must contain at least one tracked file.