How to Add an Empty Directory in Git

In this post, we will learn how we can add and push an empty directory in Git.

How to Add an Empty Directory in Git

Git is a distributed version control system designed to manage software source code and other files. It was created by Linus Torvalds in 2005 and is widely used by software developers to collaborate on projects and keep track of changes to their code over time.

Version control is a way of managing changes to files over time, allowing multiple people to work on the same files without overwriting each other's changes.

It keeps track of the changes made to files and allows users to compare different versions of a file, revert to previous versions, and merge changes from different versions.

Git uses a system of snapshots and branching to manage versions of files and allows users to work offline, making it a powerful tool for distributed development.

When starting a new local git project, it is an extremely common task to start by planning the directory structure. This involves setting up empty directories and then adding the required files later.

Let us dive in.

Requirements

  1. The latest Git version installed and configured on your machine.
  2. A local and remote git repos.

Step 1 - Initialize a New Directory

The first step is to setup a new empty directory. This is where you will store other files and directories and allow git to track the changes. You can do this by running the mkdir command as shown:

mkdir [dir_name]

Where the dir_name is the name of the directory you wish to create.

For example, suppose we want to create a new directory called geekbits we can run the command as shown:

mkdir ~/geekbits

We can then view the contents of the directory as:

ls -la ~/geekbits

The resulting output is as shown:

total 8
drwxr-xr-x  2 samsepiol samsepiol 4096 Apr  7 19:16 .
drwxr-xr-x 13 samsepiol samsepiol 4096 Apr  7 19:16 ..

Step 2 Navigate Into the New Directory

Next, navigate into the newly created directory using the cd command as shown:

cd [path_to_dir]

Example:

cd ~/geekbits

Step 3 - Add a File to the Directory

To ensure that git will start keeping track of the directory, we need to add a new dummy file. This will force git to recognize the directory and not treat it as empty. We can do this by creating a .gitkeep file as shown:

touch .gitkeep

The command above should create a new file called .gtikeep.

Step 4 - Stage the File

Once we have configured the directory and ensure that Git will not treat it as empty (ignored), we can proceed and stage the directory. We can use the add command as shown:

git add .

Once completed, we can check the staging status using the command:

git status

An example output is as shown:

On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitkeep

Step 5 - Commit Changes

We can now proceed and commit the changes to the repository with the command:

git commit -m "commit message"

Step 6 - Push Changes

You can now push the changes to the remote repository with the push command as shown below:

git push origin master

Conclusion

In this guide, you learned how to add an empty git directory and allow Git to keep track of it and push it to the remote repository. This method can allow you to quickly plan and prepare for your project in advance.

We hope you enjoyed this tutorial, leave us a comment down below, suggest any changes, feedback and more.

Similar Posts

We are certain you will love this posts, check them out.

Clear Git Cache
Learn what git cache is, how you can use the `git rm` command to clear it and why you need to clear it.
Git Command Cheat sheet
In this article, you will get a list of useful commands when working with git version control. This will allow you to quickly manage your repositories in quick steps.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.