So far in this chapter, we have been talking about how we can give instructions
to Git, to tell it which files it ought to ignore instead of tracking.
In this movie, we were actually going to do the opposite.
One of the things that always surprises people who are new to Git is the fact
that Git does not track empty directories, and that's because Git is designed to
be a file-tracking system.
Its purpose is to track files and the content in those files.
So it tracks files, and it tracks the directories it takes to get to those
files, but it ignores directories that have no files at all.
Let me demonstrate. Here I am in my project folder.
I have got an empty folder here on my Desktop called pdfs, you can create a
new folder or pull this one out of the exercise files, and I am just going to drag this into assets.
Let's open up assets, and you can see that pdfs is empty.
All right, there is nothing inside of there, and I can confirm that from the
Command Line ls -la assets/pdfs/. Now these are not things in there.
There is not a file called dot and dot dot.
In Unix, this is a reference to the current directory and reference to the parent directory.
So those were not files, it means the directory is in fact completely empty.
I had already done a git status here, and it said working directory is clean.
Let's try it again, git status, working directory is still clean, it didn't list it.
Now the moment that there is a file in there, then it will suddenly keep track of it.
So the trick that we use in order to keep track of empty directories is to put a file in them.
Now if you already had planned on putting some PDF files in there, well, no problem.
Just put the PDF files in, add the PDF files to the repo, and
that directory will get put in there at the same time, it will all get added together.
But if you want to have it track an empty directory, it can't be truly empty.
It has to have some kind of file in there.
So the cheat that everyone uses is that you just put a little tiny file in there
so that it can track it, and by convention people either name that file
.gitignore to match the gitignore file or more often now they use .gitkeep, the
opposite, basically telling it, it should keep this directory.
So we just need to put a little file inside pdfs called .gitkeep, and you can do
that using just any text editor. You don't have to actually put any content on
it, it can have zero content in it, drop it in there or it can have a comment
saying I want to keep this file, something like that.
It doesn't really matter. That file will never get used for anything except for Git.
One of the ways that we can do that from Unix is we can use the touch command.
So touch is a way to just create a file that doesn't exist.
If we say assets/pdfs/.gitkeep, this will create the file .gitkeep with no
content in it whatsoever. This is a little Unix trick.
We will do that, and now if I go back here and do ls -la assets/pdfs/, you can
see that .gitkeep file is there. It's 0 length, there is nothing inside of it.
But the file is there.
Now we can't see it, right here from the desktop, it still looks empty, but when
we go to Git and ask about it, Git says, "Ah!
I see this directory," and if we say git add assests/pdfs, git status, now
here it is it says, "Ah! I found a file." Assets/pdfs/.getkeep.
That's the file that it wants to add.
So let's go ahead and commit it, so git commit and "Add 'empty' directory with .gitkeep file in it".
So there it is now it's added to our repo, git status, and it's been tracked.
So even though it's a little bit surprising at first, it's a pretty easy fact to remember.
Git keeps track of files, not directories.
The directories are just incidental as the path to get to those files.
If we want to keep track of a directory, it has to have a file in it, no matter
how small, but there has to be a file.