Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Add an Empty Directory in Git - phoenixNAP

How to Add an Empty Directory in Git – phoenixNAP

Getting Started

Git is a version control system designed to help developers keep track of the changes they make to their code. Git tracks all changes made to the local repository and allows users to push (synchronize) changes to a remote repository.

When preparing the local repository for the next project, it is common practice to structure the directory hierarchy in advance. However, Git ignores empty directories when pushing changes to a remote repository.

In this tutorial, you’ll learn how to add and send an empty directory in Git.

prerequisites

Git

  • installed and configured (follow our tutorials and install Git on Windows, macOS, Ubuntu, CentOS 7 or CentOS 8).
  • A local and remote Git repository.

How to add an empty directory in Git?

Working in a local repository, open a command

prompt (terminal on Linux and macOS or Git Bash on Windows) and follow the steps below to create and submit an

empty directory in Git.

Step 1: Create New

Directory

Using the mkdir command, create a new empty directory in the local repository. The syntax is

: mkdir [directory-name] For example, we will create a new directory called pnap: mkdir pnap

Enumerating the repository contents using the ls command shows that the directory has been

created:

Step 2

: Move to the new directory Move to the

new directory using the cd: cd [

directory-path] command For example, to move to the

pnap directory

we created in the previous step, Run

: PNAP CD

Command Prompt now works in the pnap directory

.

Step 3: Add

a file to

the directory Instruct Git to track the new directory by creating a dummy file using the touch command. The purpose of the file is only to help Git recognize an empty directory, which it would otherwise ignore. A common practice is to create a .gitkeep or .placeholder file.

Such files are invisible to most systems by default, but they force Git to crawl them. Some users prefer the .placeholder file because the .git prefix convention is reserved for files and directories that Git uses for configuration purposes.

Run the following command to create the file: tap .placeholder

The command creates an empty file named .placeholder.

Step 4: Stage File

Run the following command to staging the file and updating the Git index: git

add.

Check if the file has been staged by running

: git status The file and directory

are now in the Git tracking index

.

Step 5: Make

a commit

Confirm the file and directory using the following syntax:

git commit -m “message”

For “message“, specify a description for the commit. For example

: git commit -m “Directory hierarchy created”

Step 6: Push changes Push

the

commit with the empty Git directory to a remote repository on GitHub. Push allows you to share your changes with teammates in the remote repository. The syntax is:

git push [remote-name

] For [remote-name], specify the name of the remote repository. For example:

git push source

The command pushes the empty directory (and the .placeholder file) to the remote repository, making the directory visible to everyone working on the project.

Conclusion

This guide showed how to add an empty directory in Git and synchronize it with a remote repository. Although the method is only a solution created by the community, it helps developers prepare the project by structuring it in advance.

Learn more about Git in our tutorial how Git works, or learn how to use Git effectively with our beginner’s guide.

Contact US