1

When I run git status git doesn't show the empty directory.
My remote repository is in github.
Can't I add and push empty directory?

3 Answers 3

4

The usual way to force adding an empty directory in systems that ignores empty directories is to add a hidden file in each such directory:

$ touch directoryname/.empty_dir
$ git add directoryname/.empty_dir
$ git push

With git, you may choose to use .gitignore in place of .empty_dir above (really, any name will do, and the only reason to use a hidden filename is to avoid seeing it when you list the directory's contents with ls).

2

You are not supposed to. But you can always add an empty readme.md or .gitignore to be able to do so.

See https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F.

0

For Linux / MAC OSX, use the following terminal's one-liner, to create / put empty placeholder files into the folders, recursively, to prevent them from getting ignored, by git:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;

P.S. For Windows, install "Git for Windows" from https://www.git-scm.com/download/win ( accept its default recommendations, while setup ) and then :

  1. Open File Manager & navigate to your Git Folder & Right Click on an empty space, to choose "Open Git Bash here"

  2. Copy the above command and paste it in the "MINGW..:Your_Git_Folder_Path" window and press Enter / Return key to run it. That's it.

Screenshots Below :

Git Bash

Hope this helps someone.

1
  • 1
    To make it not even enter ./.git: find . -path ./.git -prune -o -type d -empty -exec touch {}/.gitkeep \;
    – Kusalananda
    Commented Nov 30, 2023 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .