82

I have data directory in project's root. It has images directory and some files. Here is example:

data
├── images
│   ├── image1.jpg
│   ├── image2.jpg
│   └── image3.jpg 
├── results.csv
└── r.txt

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

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

data/
└── images/

So, I just want empty folder structure to be commited.

Bitswazsky
  • 2,468
  • 2
  • 21
  • 39
Иван Бишевац
  • 12,001
  • 20
  • 60
  • 88
  • 13
    See: [How do I add an empty directory to a git repository](http://stackoverflow.com/q/115983/1402846). – Pang Jun 30 '13 at 10:05

9 Answers9

123

Just add a file .gitkeep in every folder you want committed.

On windows do so by right clicking when in the folder and select: Git bash from here. Then type: touch .gitkeep

Joost van der Laan
  • 2,118
  • 1
  • 15
  • 9
  • 12
    What about `.keep` instead? It is agnostic to git. – Acumenus Jan 29 '14 at 04:35
  • 4
    Yes, it actually doesn't matter that much how you'll call the file – Joost van der Laan Mar 26 '14 at 22:10
  • 21
    The `git` prefix, though, gives a hint of why there's an otherwise pointless empty file lying around. – Álvaro González Apr 15 '14 at 10:49
  • 2
    The git prefix is intended for git-generated files. Just landed here from this other thread discussing this exact issue: http://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository – Josep Valls Apr 29 '15 at 20:23
  • 1
    The `.git` prefix is assumed to mean git will process it in a special way, so it is confusing. It would be better to use `.keep` and place text inside the file written in plain English explaining the purpose of the file. Even though `.keep` is not immediately obvious what it means, that is fine as we are programmers after all, and it is easy to learn what it means by looking inside the file. Once you understand what it means it is a memorable name. Go for names that are short and memorable. After a time, the brevity of it allows your mind to process it more quickly than `.keep_dir_in_git`. – still_dreaming_1 Oct 26 '15 at 17:06
  • 1
    I found it useful and time saving to put a dummy file in each directory. I say each directory, just incase the dir is not empty now, but may be in future. Use: `find . -type d -exec touch {}/.gitkeep \;` – redfox05 Dec 09 '15 at 16:29
  • This won't work if your website expects the folder to be empty. – kloddant Sep 20 '18 at 21:39
  • thanks for the tip – Luke Feb 07 '19 at 12:00
100

In Git, you cannot commit empty folders, because Git does not actually save folders, only files. You'll have to create some placeholder file inside those directories if you actually want them to be "empty" (i.e. you have no committable content).

Nevik Rehnel
  • 40,995
  • 6
  • 55
  • 46
  • Other solution for that is add a README file into empty directory. The solution is explain [here](http://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep) – Guillermo Zacur Apr 11 '15 at 16:57
  • If you missed Pang's comment on the OP above, see http://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository – Jonathan Ben-Avraham Nov 23 '15 at 09:01
51

This is easy.

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

Contents of the top-most .gitignore:

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

In the nested images folder this is your .gitignore:

# ignore everything except .gitignore
*
!.gitignore

Note, you must spell out in the .gitignore the names of the folders you don't want to be ignored in the folder where that .gitignore is located. Otherwise they are, obviously, ignored.

Your folders in the repo will, obviously, NOT be empty, as each one will have .gitignore in it, but that part can be ignored, right. :)

ddotsenko
  • 4,532
  • 21
  • 23
  • actually, this is the solution to the question, as it spares you with the hassle to deal with new files inside the folder in question. empty folders are often used for temporarily created files, which might cause problems you're dealing with git commands on other systems on which the repo gets checked out. – Dennis Winter Oct 20 '15 at 09:04
  • Im confused, why would you ignore everything apart from gitignore? Im trying to understand, but it doesnt make sense. I ended up going with `find . -type d -exec touch {}/.gitkeep \;` but it would be nice to understand this alternative, as it seems a popular upvoted one. – redfox05 Dec 09 '15 at 16:36
  • @redfox05 the OP needs at least one file in those directories so that git will include the empty folder structure. If we do not un-ignore the .gitignore file, the folder will be empty and git will not include it. – Jannie Theunissen May 16 '16 at 18:14
36

Recursively create .gitkeep files

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;
emj365
  • 1,500
  • 1
  • 15
  • 21
7

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

Then when I put stuff in that's ready to commit, I can simply remove the empty.txt file and commit the real files.

i.e.

  • data/
    • images/
      • empty.txt
Karl Nicoll
  • 14,491
  • 3
  • 46
  • 60
4

Consider also just doing mkdir -p data/images in your Makefile, if the directory needs to be there during build.

If that's not good enough, just create an empty file in data/images and ignore 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"
Kalle Pokki
  • 4,489
  • 2
  • 14
  • 17
3

You can make an empty commit with git commit --allow-empty, but that will not allow you to commit an empty folder structure as git does not know or care about folders as objects themselves -- just the files they contain.

David Culp
  • 4,816
  • 3
  • 20
  • 32
1

According to their FAQ, GIT doesn't track empty directories.

Git FAQ

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

Basically if you want to track an empty directory you can place a .gitkeep file in there. The file can be blank and it will just work. This is Gits way of tracking an empty directory.

Another option is to provide documentation for the directory. You can just add a readme file in it describing its expected usage. Git will track the folder because it has a file in it and you have now provided documentation to you and/or whoever else might be using the source code.

If you are building a web app you may find it useful to just add an index.html file which may contain a permission denied message if the folder is only accessible through the app. Codeigniter does this with all their directories.

JoeMoe1984
  • 1,812
  • 3
  • 21
  • 30
1

Simply add file named as .keep in images folder.you can now stage and commit and also able to add folder to version control.

Create a empty file in images folder $ touch .keep

$ 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"

uruapanmexicansong
  • 2,029
  • 12
  • 18
Nayagam
  • 1,320
  • 13
  • 11