GitHub is an essential tool for developers, providing a platform to store, share, and collaborate on code. One common question among users is how to create an empty directory or folder in a GitHub repository. GitHub does not support creating empty directories directly. However, there’s a workaround to this using a dummy file, usually a .gitkeep file. In this article, we’ll explore the steps and reasoning behind this method.
Why Doesn’t GitHub Support Empty Directories?
Git, the underlying version control system upon which GitHub is based, tracks file changes. Empty directories don’t contain any files, so Git doesn’t track them. Therefore, if you were to create an empty directory on your local machine and try to push it to GitHub, the directory would be ignored.
The .gitkeep Convention
The .gitkeep file is not an official Git feature, but it’s a widely accepted convention within the community. This file doesn’t have any special properties or functionalities. Its sole purpose is to be a placeholder, allowing developers to commit and push otherwise empty directories to their repositories. The name .gitkeep was chosen because it clearly communicates the purpose of the file: to “keep” the directory in Git.
Create Empty Directory via Command Line
Here’s a step-by-step guide to creating an empty directory in a GitHub repository using the .gitkeep method:
Create the Directory and .gitkeep File: For example, you want to add an empty `logs` directory in your project:
mkdir logs
touch logs/.gitkeep
Commit and Push the Changes:
git add .
git commit -m "Added empty directory"
git push origin master
Note: If your default branch is named differently, like main, replace master with your default branch name in the git push command.
Create Empty Directory via Github Dashboard
To create an empty directory directly through the GitHub web dashboard (the user interface you see when you visit a repository on GitHub.com), you’d typically create a new file and use a slash (/) to imply a directory. Unfortunately, GitHub does not allow creating purely empty directories without adding a file to them.
Here’s a step-by-step guide to create a directory (with a dummy file) via the GitHub dashboard:
- Navigate to Your Repository:
- Open the GitHub repository where you want to create the new directory.
- Add a New File:
- Click on the Add file button and then select Create new file.
- Name the File:
- In the name field, type the name of your new directory, followed by a `/`. For instance, if you want to create a directory named `logs`, type `logs/`. You’ll notice that GitHub recognizes this as a directory.
- After the `/`, type the name of a new file that will reside inside that directory. Many people use `.gitkeep` (although this name has no special significance to Git or GitHub; it’s just a convention). So, you could name it `logs/.gitkeep`.
- Commit the New File:
- Scroll down to the bottom of the page, enter your commit message, and decide whether you want to commit directly to the main branch or create a new branch and start a pull request.
- Click on `Commit changes…`
Now, your repository will have a new directory named `logs` with a single file in it named .gitkeep.
Verify on GitHub
- Navigate to your repository on GitHub.
- You should now see your directory listed, and inside it will be the `.gitkeep` file.
Alternative Filenames
While `.gitkeep` is a popular convention, some developers use other placeholder filenames like `.placeholder`. It doesn’t matter which name you choose; what’s essential is that there’s a file within the directory so Git can track it.
Conclusion
Although GitHub doesn’t support creating empty directories directly, the `.gitkeep` convention offers an effective workaround. By simply adding a placeholder file to your directory, you can ensure Git tracks it and displays it in your GitHub repository. This practice, while not official, is widely recognized in the developer community, making it a reliable method to structure your projects as needed.