How can I add an empty directory (that contains no files) to a Git repository?
You could always put a README file in the directory with an explanation of why you want this, otherwise empty, directory in the repository.
As mentioned it's not possible to add empty directories, but here is a one liner that adds empty .gitignore files to all directories.
ruby -e 'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each { |f| FileUtils.touch(File.join(f, ".gitignore")) if File.directory?(f) }'
I have stuck this in a Rakefile for easy access.
Many have already answered this question. Just adding a PowerShell version here.
Find all the empty folders in the directory
Add a empty .gitkeep file in there
Get-ChildItem 'Path to your Folder' -Recurse -Directory | Where-Object {[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0} | ForEach-Object { New-Item ($_.FullName + "\.gitkeep") -ItemType file}
Let's say you need an empty directory named tmp :
$ mkdir tmp
$ touch tmp/.gitignore
$ git add tmp
$ echo '*' > tmp/.gitignore
$ git commit -m 'Empty directory' tmp
In other words, you need to add the .gitignore file to the index before you can tell Git to ignore it (and everything else in the empty directory).
The solution of Jamie Flournoy works great. Here is a bit enhanced version to keep the .htaccess
:
# Ignore everything in this directory
*
# Except this file
!.gitignore
!.htaccess
With this solution you are able to commit a empty folder, for example /log
, /tmp
or /cache
and the folder will stay empty.
Create an empty file called .gitkeep
in the directory, and add that.