Git

Use .gitkeep to commit & push an Empty Git Folder Directory

As we know that Git cannot add an empty directory nor include an empty folder while pushing to a remote hosting service like GitHub. However, that causes an issue. To overcome this frequently occurring problem, Git offers a “.gitkeep” file. The main purpose of creating this type of file is to allow Git to push an empty folder with ease.

This manual will illustrate the procedure to use .gitkeep to commit & push an empty Git folder or directory!

How to Use .gitkeep to commit & push an Empty Git Folder or Directory?

To use .gitkeep to commit & push an empty Git folder or directory, first, navigate to the local directory, and create a new directory inside the repository. Then, add and move inside the created directory using the “cd” command. Next, make a “.gitkeep” file and move it back to the main directory. After that, commit modifications and push it to the remote.

To implement the above-listed steps, move ahead!

Step 1: Move to Git Directory

First, navigate to the local remote repository with the help of provided command:

$ cd "C:\Users\nazma\Git\demo10"

Step 2: Create Directory

Next, execute the “mkdir” command to make a new directory in the Git repository

$ mkdir new-dir

Step 3: Add Changes to Repository

Then, add all the made changes to the Git repository with the help of the following command:

$ git add .

Step 4: Move to Directory

After that, move into the newly created directory by executing the “cd new*” command:

$ cd new*

Step 5: Create .gitkeep File

Now, run the “touch” command to create a “.gitkeep” file:

$ touch .gitkeep

Step 6: Move Back to Repository

Next, revert to the Git main working repository through the below-provided command:

$ cd ..

Step 7: Commit Changes

Finally, run the “git commit” command to update the Git repository by committing all of the added changes:

$ git commit -m "new_git_directory"

Step 8: Push Empty Directory

Lastly, push an empty directory to the remote directory using the below-stated command:

$ git push origin

It can be seen that we have got the “Everything up-to-date” message because we have already pushed the empty directory to the specific remote(origin):

We have compiled the method to use .gitkeep to commit & push an empty Git folder or directory using easy steps.

Conclusion

To use .gitkeep to commit & push an empty Git folder or directory, first, run the “cd” command with the specified Git directory name. Then, create a new directory inside the repository. Next, add and move inside the created directory and create a “.gitkeep” file and move it back to the main directory. Lastly, commit add modifications, and push it to the Git remote repository. This tutorial explained the use of .gitkeep to commit & push an empty Git folder or directory.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.