There is no Git feature that supports the creation of an empty folder. To create a folder in Git, you must commit a folder to a repository that contains at least one file, even if that file is hidden.
In this guide, we’re going to discuss how to create a folder on GitHub. We’ll walk through an example of how to create a folder from the Git command line, and give instructions on how to create a folder from the GitHub web interface.
Create a Folder in GitHub: From Git
We’re going to start work on an update for a project called ck-git. We already have a Git repository set up for this project. Before we start working on our updated code, we’re going to create the directory structure for our code.
Our GitHub repository currently contains no folders:
We want to create a folder called
docs and a folder called
dev. To do this, we’re going to use the mkdir command:
mkdir docs dev
This command creates our two directories. If we add these folders to a commit and push our changes, they will not show up on GitHub. This is because GitHub does not display empty folders.
To create our new folders, we need to add contents to our folders. Because we are only initializing our project, we can add in a few blank single files.
These blank files will let us create our folder without having to actually start work on our updates before we have created the directory structure and uploaded it to Git.
Let’s create two blank files to populate our new folders:
touch docs/.gitkeep touch dev/.gitkeep
We have created two files called
.gitkeep. Our two new directories now contain one file each. The
.gitkeep file name is often used to create blank folders. However, there is no requirement that the name of your file needs to be
.gitkeep to create a folder.
Now that these files exist, we can track our otherwise empty folders and commit our code:
git add * git commit -m "feat: Create directory structure" git push
The git add command adds our files to the staging area. The git commit command creates a commit with our changes. The git push command pushes our changes to our remote repository. Our remote is hosted on GitHub.
Let’s see what happens when we run these commands:
[master f10ed95] feat: Create directory structure 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 dev/app.py create mode 100644 docs/Start_Here.md ...
Our code successfully changes our files. We now have two new folders in our GitHub project: dev and docs. We can check that our folders exist by navigating to the GitHub dashboard for our project:
Each of these folders contains one file. Now that we’ve defined the directory structure, we are free to continue working on our project.
It’s not always necessary to upload empty folders before you have worked on them. If you need to for some reason, such as if you are required to show your new structure to other collaborators, these are the steps you should follow if you want to add the folders from Git.
Create a Folder in GitHub: From the Web Dashboard
There is no user interface feature to create a blank folder in GitHub. To create a new folder, you need to create a new file and specify the directory in which that file should appear.
In other words, you can only create a folder after you have created a file.
We want to add one more directory to our code called “assets”. To start, let’s navigate to our repository and click on the “New file” button.
In the top text field, we’re going to create a file called “assets/.gitkeep”:
This will let us create a folder called
assets and a file called
.gitkeep in that folder. Git automatically displays the new folder path when you type in a
Optionally, you can add text or code to the file that you have created. Again, “.gitkeep” is only a placeholder file name. You can name your file whatever you want.
Commit new file. This will add our new file to our repository. If we go back to our main folder, we can see that our new folder and file have been created:
We’ve now successfully created a folder in GitHub.
GitHub does not allow you to add blank folders to your Git repository. A folder must contain a file before you can add it to a repository. If you want to create a new folder, create it first and then add a placeholder file into that folder. Then, add the new folder and file to your Git repo.
Now you have the tools you need to create a folder in GitHub like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more