How do I add an empty directory (that contains no files) to a Git repository?
How do I add an empty directory (that contains no files) to a Git repository?
Another way to make a directory stay (almost) empty (in the repository) is to create a .gitignore file inside that directory that contains these four lines:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Then you don't have to get the order right the way that you have to do in m104's solution.
This also gives the benefit that files in that directory won't show up as "untracked" when you do a git status.
Making @GreenAsJade's comment persistent:
I think it's worth noting that this solution does precisely what the question asked for, but is not perhaps what many people looking at this question will have been looking for. This solution guarantees that the directory remains empty. It says "I truly never want files checked in here". As opposed to "I don't have any files to check in here, yet, but I need the directory here, files may be coming later".
You can't. See the Git FAQ.
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 <dir>" and it will add 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; you can leave it empty, or fill in the names of files you expect to show up in the directory.
First things first:
An empty directory cannot be part of a tree under the Git versioning system.
It simply won't be tracked. But there are scenarios in which "versioning" empty directories can be meaningful, for example:
cache/ or logs/ directories, where we want to provide the folder but .gitignore its contentsMany users suggest:
README file or another file with some content in order to make the directory non-empty, or.gitignore file with a sort of "reverse logic" (i.e. to include all the files) which, at the end, serves the same purpose of approach #1.While both solutions surely work I find them inconsistent with a meaningful approach to Git versioning.
.gitignore to do a thing (keeping files) that is the very opposite of what it's meant for (excluding files), even though it is possible?Use an empty file called .gitkeep in order to force the presence of the folder in the versioning system.
Although it may seem not such a big difference:
You use a file that has the single purpose of keeping the folder. You don't put there any info you don't want to put.
For instance, you should use READMEs as, well, READMEs with useful information, not as an excuse to keep the folder.
Separation of concerns is always a good thing, and you can still add a .gitignore to ignore unwanted files.
Naming it .gitkeep makes it very clear and straightforward from the filename itself (and also to other developers, which is good for a shared project and one of the core purposes of a Git repository) that this file is
I've seen the .gitkeep approach adopted by very important frameworks like Laravel, Angular-CLI.
Andy Lester is right, but if your directory just needs to be empty, and not empty empty, you can put an empty .gitignore file in there as a workaround.
As an aside, this is an implementation issue, not a fundamental Git storage design problem. As has been mentioned many times on the Git mailing list, the reason that this has not been implemented is that no one has cared enough to submit a patch for it, not that it couldn’t or shouldn’t be done.
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).
Add a .gitkeep file inside that directory and commit it.
touch .gitkeep
it is the standard followed by git.
I've been facing the issue with empty directories, too. The problem with using placeholder files is that you need to create them, and delete them, if they are not necessary anymore (because later on there were added sub-directories or files. With big source trees managing these placeholder files can be cumbersome and error prone.
This is why I decided to write an open source tool which can manage the creation/deletion of such placeholder files automatically. It is written for .NET platform and runs under Mono (.NET for Linux) and Windows.
Just have a look at: http://code.google.com/p/markemptydirs
This solution worked for me.
.gitignore file to your empty directory:*
*/
!.gitignore
* ignore all files in the folder*/ Ignore subdirectories!.gitignore include the .gitignore filegit rm -r --cached .
git add . // or git stage .
git commit -m ".gitignore fix"
git push
When you add a .gitignore file, if you are going to put any amount of content in it (that you want Git to ignore) you might want to add a single line with just an asterisk * to make sure you don't add the ignored content accidentally.
Reading @ofavre's and @stanislav-bashkyrtsev's answers using broken GIT submodule references to create the GIT directories, I'm surprised that nobody has suggested yet this simple amendment of the idea to make the whole thing sane and safe:
Rather than hacking a fake submodule into GIT, just add an empty real one.
A GIT repository with exactly one commit:
commit e84d7b81f0033399e325b8037ed2b801a5c994e0
Author: Nobody <none>
Date: Thu Jan 1 00:00:00 1970 +0000
No message, no committed files.
To add an empty directory to you GIT repo:
git submodule add https://gitlab.com/empty-repo/empty.git path/to/dir
To convert all existing empty directories to submodules:
find . -type d -empty -delete -exec git submodule add -f https://gitlab.com/empty-repo/empty.git \{\} \;
Git will store the latest commit hash when creating the submodule reference, so you don't have to worry about me (or GitLab) using this to inject malicious files. Unfortunately I have not found any way to force which commit ID is used during checkout, so you'll have to manually check that the reference commit ID is e84d7b81f0033399e325b8037ed2b801a5c994e0 using git submodule status after adding the repo.
Still not a native solution, but the best we probably can have without somebody getting their hands really, really dirty in the GIT codebase.
You should be able to recreate this exact commit using (in an empty directory):
# Initialize new GIT repository
git init
# Set author data (don't set it as part of the `git commit` command or your default data will be stored as “commit author”)
git config --local user.name "Nobody"
git config --local user.email "none"
# Set both the commit and the author date to the start of the Unix epoch (this cannot be done using `git commit` directly)
export GIT_AUTHOR_DATE="Thu Jan 1 00:00:00 1970 +0000"
export GIT_COMMITTER_DATE="Thu Jan 1 00:00:00 1970 +0000"
# Add root commit
git commit --allow-empty --allow-empty-message --no-edit
Creating reproducible GIT commits is surprisingly hard…
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}
An easy way to do this is by adding a .gitkeep file to the directory you wish to (currently) keep empty.
See this SOF answer for further info - which also explains why some people find the competing convention of adding a .gitignore file (as stated in many answers here) confusing.
You can't. This is an intentional design decision by the Git maintainers. Basically, the purpose of a Source Code Management System like Git is managing source code and empty directories aren't source code. Git is also often described as a content tracker, and again, empty directories aren't content (quite the opposite, actually), so they are not tracked.
Just add empty (with no content) .gitignore file in the empty directory you want to track.
E.g. if you want to track empty dir /project/content/posts then create new empty file /project/content/posts/.gitignore
Note: .gitkeep is not part of official git:
To extend Jamie Flournoy's solution to a directory tree, you can put this .gitignore in the top-level directory and touch .keepdir in each subdirectory that git should track. All other files are ignored. This is useful to ensure a consistent structure for build directories.
# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/
# Git doesn't track empty directories, so track .keepdir files, which also
# tracks the containing directory.
!.keepdir
# Keep this file and the explanation of how this works
!.gitignore
!Readme.md
I search into this question becuase: I create a new directory and it contains many files. Among these files, some I want to add to git repository and some not. But when I do "git status". It only shows:
Untracked files:
(use "git add <file>..." to include in what will be committed)
../trine/device_android/
It does not list the seperate files in this new directory. Then I think maybe I can add this directory only and then deal with the seperate files. So I google "git add directory only".
In my situation, I found I can just add one file in the new directory that I am sure I want to add it to git.
git add new_folder/some_file
After this, "git status" will show the status of seperate files.
Just add a readme or a .gitignore and then delete it, not from terminal, from the github website, that will give a empty repository