Is it possible to add an empty directory to a Git repository?

Asked

Viewed 135 times

1

I wonder if I can add an empty directory to my Git repository? If possible, how to do this?

  • You want that directory to be empty forever?

  • No, just a question... if it is possible to do this.

  • 2

    @Bruno Sometimes it is interesting (simplifies the code) to make sure that a folder exists, even if you don’t have anything to put in it at first. I think it’s pertinent, I’ve had this doubt with other Vcss.

  • I don’t think we can make it empty, but it makes it empty.

2 answers

3


According to the documentation is not possible.

Can I add Empty Directories?

Currently the design of the Git index (staging area) only Permits files to be Listed, and nobody competent enough to make the change to allow Empty Directories has Cared enough about this Situation to Remedy it. Directories are Added Automatically when Adding files Inside them. That is, Directories Never have to be Added to the Repository, and are not tracked on their Own. You can Say "git add " and it will add the files in there. If you really need a directory to exist in checkouts you should create a file in it. . gitignore Works well for this purpose (there is also a tool Markemptydirs using the . NET framework which Allows you to automate this task); you can Leave it Empty or Fill in the Names of files you do not expect to show up in the directory.

It has however this "hack" that allows you to achieve almost what you want. Create a file with name .gitignore with this content. This prevents new files from being created in the directory.

# Ignore everything in this directory
*
# Except this file
!.gitignore
  • This is exactly what I had just accessed :D

  • 1

    We can leave it empty.

0

A common way to do this (more or less) is to add a file in to the folder. It is common to see repositories with folders that only have a blank file .gitkeep within.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.