How to add an empty directory to a remote git repository?

Asked

Viewed 2,639 times

1

I have a PHP application that needs to upload photos to a remote directory, but this directory needs to be created earlier in the repository so that these files can be uploaded.

But when I send this empty directory, the repository does not recognize.

How can I send an empty directory to the git remote repository?

  • 3

    Maybe it’s important, but just out of curiosity, why would you want to browse an empty directory?

  • Very right. I added an example to help you understand. Thank you!

  • Related: https://answall.com/q/256998/64969

2 answers

5

Git doesn’t allow empty directories. But you can generate a hidden file within the directory to upload that directory.

Most people do this procedure by generating a file called .gitignore

This way, if the directory date is empty, you can still upload the directory, with the following command line:

touch data/.gitignore

Then performing the command git commit -a your remote repository will be updated with the hidden file inside the desired directory.

  • 1

    It is clear that the file .gitignore has a special function, which is to define which directory files (or descendants) should be ignored in the repository. And it is not generally recommended to have several .gitignore in the same project, although it is possible.

4

The archive .gitkeep, which can be used in your project when, for several different reasons you want to create an empty directory and commit it at a certain point of development. Ex: When you create a directory to save files like photos, Pdfs and so on...

The archive .gitkeep is only a convention between developers, there being no mention of this (to date) on the site git officer, but as it is a standard among developers, it serves for you to "pass a message" to others who will see your project in the future.

Create within your project the directory you want to commit and within this directory create a file called . gitkeep, leaving something like this...

project-dir/
├── .git
├── other-dir
└── untracked-dir
    └── .gitkeep

Okay, now you can run the commands git add [seus arquivos] and git commit -m [mensagem de commit]. Now when another developer comes across this file he will know what it is about!

Browser other questions tagged

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