Commit empty folder structure (using git)

I have a data directory in the root of the project. He has a catalog of images and some files. Here is an example:

  • Data /
    • Images /
      • image1.jpg
      • image2.jpg
      • image2.jpg
    • results.csv
    • r.txt

What to write in gitignore, ignore files from data / directory (i.e. results.csv and r.txt) and files from images / directory (image.jpg, image2.jpg, image3.jpg)?

When I commit it, the folder structure in the repository should be:

  • Data /
    • Images /

So, I just want the empty folder structure to be launched.

+65
git gitignore
Jan 26 '13 at 20:27
source share
9 answers

In Git, you cannot commit empty folders, because Git does not actually save folders, but only files. You will need to create some placeholder file inside these directories if you really want them to be "empty" (i.e. you do not have compromising content).

+85
Jan 26 '13 at 20:28
source share

Just add the .gitkeep file to each folder you want to commit.

In windows, do this by right-clicking in the folder and select: Git bash from here. Then type: touch .gitkeep

+108
Nov 11 '13 at 20:08
source share

It is easy.

tell .gitignore ignore everything except .gitignore and the folders you want to keep. Put .gitignore in the folders you want to save in the repo.

The contents of the top .gitignore :

 # ignore everything except .gitignore and folders that I care about: * !images* !.gitignore 

In the images subfolder, this is your .gitignore :

 # ignore everything except .gitignore * !.gitignore 

Please note that you must specify the names of folders in .gitignore that you do not want to ignore in the folder where this .gitignore is located. Otherwise, they are obviously ignored.

Your folders in the repo will obviously NOT be empty, as each of them will have .gitignore , but this part can be ignored, right. :)

+48
Jan 26 '13 at 11:50
source share

Create .gitkeep files recursively

 find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
+30
Jul 06 '15 at 15:38
source share

Traditionally, whenever I wanted to commit to an empty directory structure, I create a structure and then place an empty file called empty.txt in the leaf directories.

Then, when I put things ready for commit, I can just delete the empty.txt file and commit the real files.

i.e.

  • Data /
    • Images /
      • empty.txt
+7
Jan 26 '13 at 23:55
source share

Consider also that you do mkdir -p data/images in your Makefile if the directory should be there at build time.

If this is not enough, just create an empty file in data / images and ignore the data.

 touch data/images/.gitignore git add data/images/.gitignore git commit -m "Add empty .gitignore to keep data/images around" echo data >> .gitignore git add .gitignore git commit -m "Add data to .gitignore" 
+4
Jan 27 '13 at 6:56
source share

You can do an empty commit using git commit --allow-empty , but this will not allow you to git commit --allow-empty an empty folder structure, since git does not know or care about folders as objects themselves - only the files they contain.

+3
Jan 26 '13 at 20:42 on
source share

According to their FAQ, GIT does not track empty directories.

Git Frequently Asked Questions

However, there are workarounds based on your needs and your project requirements.

Basically, if you want to track an empty directory, you can place the .gitkeep file .gitkeep . The file may be empty and it will work. This is the Gits way of tracking an empty directory.

Another option is to provide documentation for the catalog. You can simply add a readme file to it describing its expected use. GIT will track the folder because it has a file in it, and now you have provided you with documentation and / or someone else who can use the source code.

If you are creating a web application, it may be useful for you to simply add the index.html file, which may contain a permission denial message if the folder is accessible only through the application. Codeigniter does this with all of its directories.

+1
Nov 14 '13 at 21:59
source share

Just add the file named .keep to the image folder. Now you can perform the stage and commit, as well as add a folder to version control.

Create an empty file in the $ touch .keep image folder

$ git status

 On branch master
 Your branch is up-to-date with 'origin / master'.

 Untracked files:
   (use "git add ..." to include in what will be committed)

     images /

 nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -m "adding empty folder"

+1
Oct 06 '17 at 6:40
source share



All Articles