Git is a version control system and a code management tool. Git doesn't track empty folders or directories. Git only tracks files. Git will only include directories in version control when there are files present in that directory or folder.
For example, let's create a new directory called demoDir. When we run the Git Status command, we see that the working tree is empty. Git doesn't track the demoDir folder. Nothing can be added to the staging area by using Git Add.
mkdir demoDir
git status
Output:
On branch feature
nothing to commit, working tree clean
However, if we add a file to demoDir, Git starts tracking the folder.
touch demoDir/demoFile.txt
git status
Output:
Untracked files:
(use "git add <file>..." to include in what will be committed)
demoDir/
nothing added to commit but untracked files present (use "git add" to track)
There can be a few cases where we want Git to track an empty directory.
Some projects require a predefined folder structure, and this structure may include empty directories.
Some projects may not work without the presence of certain folders.
We may also need a folder to store temporary files(like cache and logs). We want Git to track the folder but ignore the files it contains by using .gitignore.
There are more scenarios where we may require Git to track empty directories or folders. There is no direct way to make Git track empty folders. Let's learn how to add an empty folder to a Git Repository.
The only way to make Git track an empty directory is to add a file in that directory. We can add a README file to the directory and add some content to inform others about its use. The only use of the README file is to make sure that Git tracks the directory or folder where it is present.
Let's create a simple README.txt file inside the demoDir folder to make Git track demoDir.
touch demoDir/README.txt
We can add some text to the README.txt file to inform other users about its purpose.
README.txt:
​
This README file is added to make sure Git tracks the demoDir directory. The README file serves no other purpose.
The .gitignore file tells Git to ignore(or not track) some files. We can add a .gitignore file to an empty directory to make Git track the directory. If the empty directory or folder is meant to fill up in the future with new files, but we want to ignore these new files, then .gitignore is the perfect solution. It will make sure that the directory is tracked, but none of the contents of the folder should be tracked.
Let's create a .gitignore file in our demoDir. This allows us to track the empty directory.
touch demoDir/.gitignore
If we know that files will be added to the directory in the future, but we want to ignore them, then write the following lines in the .gitignore file.
*
# * to ignore everything
!.gitignore
# !.gitignore to not ignore the .gitignore file
A well-known solution is to create a .keep or a .gitkeep file inside the folder. The .keep or the .gitkeep file informs other developers, collaborators, or users of the repository that this is an empty folder tracked by Git. The only purpose of the .gitkeep or .keep file is to make sure that Git keeps or tracks the otherwise empty directory. The .keep or .gitkeep is not a special name but just a simple convention that is recognized by others. The .keep or .gitkeep doesn't have any special meaning for Git.
Let's add a .gitkeep file in the demoDir to make Git track it.
touch demoDir/.gitkeep
Git is a version control system. We can track our files using Git. However, Git doesn't track empty directories or folders. Git will only track a directory or folder when it contains a file. We can add a dummy file or a README file to an empty directory to make Git track the folder. Or we can use the .gitignore file. The .gitignore file should be used when new files are meant to be added to the folder, and we want to ignore these new files but track the folder.
The best method is to create a .keep or a .gitkeep file. The sole purpose of this file is to make Git track the directory or folder. Also, it would be less confusing for other users of the repository, when compared to README or .gitignore file. The primary purpose of the README and .gitignore file is not to make Git track an empty folder. But the .gitkeep file's sole purpose is to add an empty directory to the repository, that is, make Git track the folder. Note that .gitkeep or .keep doesn't have any special meaning in Git.