How can I add a blank directory to a Git repository?

199

Solution 1

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

Solution 2

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.

Solution 3

Create an empty file called .gitkeep in the directory, and add that.

Solution 4

You could always put a README file in the directory with an explanation of why you want this, otherwise empty, directory in the repository.

Solution 5

touch .placeholder

On Linux, this creates an empty file named .placeholder. For what it's worth, this name is agnostic to git, and this approach is used in various other places in the system, e.g. /etc/cron.d/.placeholder. Secondly, as another user has noted, the .git prefix convention can be reserved for files and directories that Git itself uses for configuration purposes.

Alternatively, as noted in another answer, the directory can contain a descriptive README.md file instead.

Either way this requires that the presence of the file won't cause your application to break.

Share:
199
Saravanan C
Author by

Saravanan C

Updated on January 26, 2022

Comments

  • Saravanan C
    Saravanan C over 2 years

    Does anybody know, If it's possible, and how to send SMS to more than one recipient with one time user interaction. Which means, Our application should send SMS to a list of numbers with static message body when the user press the send button one time on iPhone? Thanks

    • tiwo
      tiwo almost 12 years
      While it's not useful, there is a way to hack an empty (really empty) directory into your repo. It won't checkout with current versions of Git, however.
    • JBentley
      JBentley about 11 years
      @tiwo I for one disagree that it's not useful. Your directory hierarchy is part of your project, so it should be version controlled.
    • tiwo
      tiwo about 11 years
      @JonBentley sure you are right! It's a pity that git fails to respect this part - but as long as this isn't fixed, it is useless to have empty directories in a repository (note that git ignores empty directories when checking out as well)
    • Adam Marshall
      Adam Marshall about 11 years
      In my case, I'd like to add a directory structure for tmp files, but not the tmp files themselves. By doing this, my tester has the correct structure (otherwise there are errors) but I don't clog my commits with tmp data. So yes, it's useful to me!
    • Quantum7
      Quantum7 about 11 years
      @AdamMarshall I think tiwo was saying that the hack is not useful, since it is ignored by checkout. Tmp dirs do sound like a useful feature for a VCS.
    • RyPeck
      RyPeck almost 11 years
      Why not have the procedure that creates the tmp files also create the tmp directory?
    • Tobias Kienzler
      Tobias Kienzler over 10 years
      You could probably use a githook or gitattribute filter to have a .deleteme file added to the repo that on checkout gets removed automatically. It will however probably break git commit -a and potentially be triggered on every checkout...
    • DaBlick
      DaBlick about 8 years
      To those people who think this isn't a useful feature, I would issue this challenge question: I have a /temp directory that needs to be "there" when the user checks things out. The software package I'm using does not create it if it's not there. How would I do this if not via having git recognijze an empty directory.
    • Zaz
      Zaz about 7 years
    • Áxel Costas Pena
      Áxel Costas Pena almost 6 years
      I propose to add this to the main solution: stackoverflow.com/a/21422128/1670956
    • Rick O'Shea
      Rick O'Shea about 4 years
      As required targets for future file creation under various permission schemes, empty folders are clearly useful.
  • Andy Lester
    Andy Lester over 15 years
    That's exactly what I said. Both paragraphs are addressed in the snippet of FAQ I posted.
  • wnoise
    wnoise over 15 years
    I think the aside is unteresting and useful to know -- it can be fixed, just don't expect it anytime soon when there's such an easy workaround for most cases.
  • Aristotle Pagaltzis
    Aristotle Pagaltzis over 15 years
    Sorry, I didn’t read the last paragraph, and while I did read the first paragraph, well, I’m not sure why I repeated that information.
  • Michael Johnson
    Michael Johnson over 15 years
    Of course, this extra answer does serve to point out the fact.
  • Christoffer Hammarström
    Christoffer Hammarström about 14 years
    Two things: You could just "echo '*' > tmp/.gitignore" instead of touching, and "git commit -m" does not commit changes done after you've added the files to the index.
  • saeedgnu
    saeedgnu about 13 years
    +1, Good suggestion, an empty directory does not make any sense unless it is going to be used in the future. So create a README file inside it and write what this directory is for, and what files will be put there in the future. That solves both two problems.
  • Amala
    Amala about 13 years
    Below answer is MUCH better. The fact that git the low level software doesn't allow it doesn't matter to me as much as HOW to actually use Git when I need an empty directory. Adding a 2 line .gitignore seems acceptable to me.
  • ThomasH
    ThomasH over 12 years
    I contest this view. Structure is content, and everything you name contributes to content.
  • Admin
    Admin over 12 years
    I agree. Empty folders are annoying and should be explained in all properly handled repositories of any kind.
  • Dan Moulding
    Dan Moulding over 12 years
    An empty file isn't source code or content either. It's just a name. Yet Git will happily track empty files. I don't think it was an intentional design decision to make Git refuse to track empty directories. I think tracking empty directories is a feature that simply isn't needed 99% of the time, so they didn't bother to do the extra work required to make it work properly. Git can do it if someone wants the feature badly enough to implement it. I doubt the Git maintainers would be opposed to such a patch if it were done correctly.
  • Tino
    Tino over 12 years
    I'd rather use find . -type d -empty -print0 | xargs --null bash -c 'for a; do { echo "*"; echo "!.gitignore"; } >>"$a/.gitignore"; done' --
  • lulalala
    lulalala over 12 years
    Well if one want to move files into a new directory, they can't do it through git mv as git will complain that new directory is not under version control
  • ofavre
    ofavre over 12 years
    You can read "it's impossible, you can't, etc." all over the Internet for this frequent question. The .gitignore trick is a frequent answer, and satisfies many needs. However it IS possible to make git track an truly empty directory, see my answer
  • Brent Bradburn
    Brent Bradburn about 12 years
    Note: The symbolic link that I suggested is "broken" in a clean checkout because the .generated directory does not initially exist. It will no longer be broken once you do your build.
  • Jez
    Jez over 11 years
    @ilius Nonsense. A directory structure containing empty directories may be highly desirable in many situations (like an MVC app where you want a models directory but haven't gotten around to creating any models yet, or a shared views directory you plan to add shared views to, later). Moreover, putting a README in each one of these is overkill as it's obvious what they're there for, and it's easy to forget to put a README in each one of them. AND you have to remember to remove the README when you add some other files to them. Basically, git should definitely allow empty directories.
  • pedromanoel
    pedromanoel over 11 years
    I think the README solution proposed by @JohnMee should be used together with this one; the .gitignore file provides an explanation of what we want to keep out of version control, while the README file explains what is the purpose of the directory, which are both very important pieces of information.
  • Daniel Da Cunha
    Daniel Da Cunha about 11 years
    @TobyAllen here is the updated FAQ link The top answer is also what is recommended by the FAQ with more precise instructions.
  • Joe Atzberger
    Joe Atzberger almost 11 years
    @Jez: I disagree. The point is that git is designed to control (and index) source-code. Importantly, the id of a commit is a hash of the contents. That is to say, it must have contents. You don't need a README in every part of the tree, only leaf nodes. If you have places you intend to put code, but no code, and you won't even take the time to echo "place for models" >> README, then what you have is an idea not a commit. It is not of interest to git. Saying "I want the running app to have XYZ empty directories" is a runtime problem, not a source problem. Handle it w/ your installer.
  • jbo5112
    jbo5112 almost 11 years
    @JoeAtzberger What about web applications? Should I give the web/application server permission to create the directories whenever they're missing? That's a strong security problem and a small performance issue to keep checking. Should I create a web app that needs special code run (e.g. create cache folder) in addition to a git clone? Now I need to add a readme, an install script and time to deal with everyone who doesn't bother to look at anything, expecting it to just work. There are even times when a directory tree with no files is something that gets used in a build/install process.
  • jbo5112
    jbo5112 almost 11 years
    @JoeAtzberger It's a missing feature, not an intentional limitation. From 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.
  • jbo5112
    jbo5112 almost 11 years
    It's a missing feature (and low priority), not an intentional limitation. From 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.
  • Joe Atzberger
    Joe Atzberger almost 11 years
    @jbo5112 Yes, the "special code" you refer to is the "installer" I mentioned. Your webapp installation already has to handle creating a database, local config, pulling dependencies or 100 other operations, but a couple empty directories are beyond it? Try gradle, passenger, chef, a primitive Makefile, etc. There is no security difference between creating directories and the other (potentially far more complicated/dangerous) work of installing an app. And if you really have no deps, config, DB, etc., and no installer, then just use the README. No case requires you to do both.
  • Joe Atzberger
    Joe Atzberger almost 11 years
    Your FAQ citation says it is the design. It describes the current state of the code (w/o the feature). It also says of all the many git contributors, none of them care enough to change it. I would say that also characterizes their "intent" more than any feature wishlist. You may contemplate the people who wrote the best version control in the world, the one we freely enjoy: are they crazy? mean? incompetent? or do they maybe just understand this differently?
  • jbo5112
    jbo5112 almost 11 years
    @JoeAtzberger The way it appears to me is that it is a missing feature that isn't important enough for the maintainers to fix, because it would require a lot of redesign. Remember, git was built and deployed by Linus Torvalds in 2 weeks because he was frustrated with existing versioning. If it were a feature they intend to leave out, then the FAQ would probably say it's intentionally not allowed or "Permanently the design of the Git index," instead of having discussions on the mailing list about how to implement the feature that is not "Currently the design."
  • jbo5112
    jbo5112 almost 11 years
    My web-app case is that multiple short-term developers are pulling/cloning the code and hitting the same database (not ideal, but I have reasons). It has no installer; no managing of deps, config, etc.; no README. It has to stabilize and produce more installs first. Occasionally the code is pushed into production. On a separate project, I have server code that creates a lengthy directory structure (no files) with some complicated permissions for new customer accounts. This would be much easier to maintain by managing a template copy instead of code. Chef is overkill, and nothing else helps.
  • Emil Lundberg
    Emil Lundberg almost 11 years
    Though the more I think of it, the more it feels like "SHA hash of the empty string", if it exists, actually would be a well-defined identifier for an empty tree, unless it would be impossible to tell whether that object is a tree or a blob.
  • Carlos Campderrós
    Carlos Campderrós almost 11 years
    @pedromanoel I write the documentation you would put in the README inside the .gitignore file (as comments).
  • steffen
    steffen over 10 years
    You may want to ignore the .git directory: find . -name .git -prune -o -type d -empty -exec touch {}/.gitignore \;
  • Andrew
    Andrew over 10 years
    The problem with "Just add a README" solution is that many times an application is descending into the directory and setting values from (temporary) files that are put in this otherwise empty directory. I like the idea of actually forcing git to keep it empty since this may be what the application wants/needs.
  • akhan
    akhan over 10 years
    A simpler variation for most situations is find * -type d -empty -exec touch {}/.gitignore \;
  • Sukima
    Sukima over 10 years
    I've seen a lot of repos that use an empty file called .gitkeep for this purpose.
  • Gladen
    Gladen over 10 years
    Don't really agree. I can find various reasons why I want to track an empty folder. For example, I am developing a very lightweight PHP MVC framework for my projects. I have specific folders for placing models, views, etc. When I make a new site based on my framework, those folders are empty since there are no models or views by default, but I do need the folder to exist, else my framework won't work!
  • Thomas
    Thomas over 10 years
    This is great for out-of-source builds where you don't want to check in any binaries but don't want to have the user create the folder manually (in case your build system can't handle that).
  • Kenney
    Kenney over 10 years
    touch file; echo bla > file gives file: File exists here; in that case it's safest to use rm file; touch file; echo something >> file (and probably many other solutions ;-))
  • Dennis
    Dennis about 10 years
    @Jez why remove the READMEs? READMEs in directories besides just the root directory is very useful, especially when your repository is hosted on GitHub which automatically displays READMEs in every directory.
  • t-mart
    t-mart about 10 years
    .gitkeep has not been prescribed by Git and is going to make people second guess its meaning, which will lead them to google searches, which will lead them here. The .git prefix convention should be reserved for files and directories that Git itself uses.
  • Peter Perháč
    Peter Perháč about 10 years
    spot the 1 difference: 1.) an empty folder, 2.) a folder with .gitignore file in it. ;-)
  • Matt K
    Matt K about 10 years
    @CarlosCampderrós came here to say that as well! I put a .gitignore into the empty directory with one comment "# this empty directory needs to be checked into git". This answer could pose problems when you actually want to put something into the directory and forget about your clever .gitignore instructions.
  • Mild Fuzz
    Mild Fuzz about 10 years
    Just so we're all on the same page, I do not do this anymore. It's a waste of time. The .gitkeep convention is a much better practise.
  • redolent
    redolent about 10 years
    This is perfect for cache folders.
  • psyrendust
    psyrendust about 10 years
    If you just do echo bla > file you will not get file: File exists because > will overwrite the file if it's already there or create a new one if it doesn't exist.
  • moopet
    moopet almost 10 years
    I agree in some cases this is a very good idea, but in others (such as distributing a project where you have an otherwise empty skeleton with folders such as models/ and views/ ) you would want the user to have these directories at hand rather than manually having to read read the docs, and it could be a bit much to expect them to run some sort of installation script after cloning the repo. I think this answer in combination with @john-mee's README answer should cover most if not all cases.
  • AD7six
    AD7six almost 10 years
    While good advice - There is no need to ignore the file itself - either create the file and force add it (echo "*" > .gitignore; git add -f .gitignore;) or add the file and edit it (`touch .gitignore; git add .gitignore; echo "*" > .gitignore). This removes the somewhat pointless "but don't ignore the .gitignore" rule.
  • gvsrepins
    gvsrepins over 9 years
    He wants to keep a empty directory and not a file.
  • Roman
    Roman over 9 years
    And i have mentioned that it will keep the .htaccess, too. Example: if a software has a directory for log-files (like oxid eshop) that should not be accesible via web, there is a .htaccess in the directory. If you put the above mentioned .gitignore in the folder, the .htaccess will not be comitted and the folder will be accessible via web.
  • lmat - Reinstate Monica
    lmat - Reinstate Monica over 9 years
    @GreenAsJade If I understand the answer properly, this does not do precisely what the OP wanted. This creates a directory with one file in it, not an empty directory. In my case, I'm wanting an empty directory.
  • lmat - Reinstate Monica
    lmat - Reinstate Monica over 9 years
    @t-mart "The .git prefix convention should be reserved..." Why? Does git request this reservation?
  • lmat - Reinstate Monica
    lmat - Reinstate Monica over 9 years
    After putting the empty folder into the index and committing, is it then possible to git svn dcommit it with the desired result?
  • GreenAsJade
    GreenAsJade over 9 years
    "Empty in the repo". In your working copy, yes, you have the .gitignore file. Most people consider .git-something files to be "not there" for working purposes: IE for all intents and purposes it's considered an empty directory.
  • Giacomo1968
    Giacomo1968 over 9 years
    This is good for an initial bare directory, but what if it starts to fill with files? Then Git will notice them and claim them as untracked files. The selected answer here works far more elegantly to allow one to keep a directory but then safely ignore the contents.
  • Asclepius
    Asclepius over 9 years
    The question and the predominant general concern is about adding an empty directory. If it later has a resident file, obviously delete the .keep file or just disregard it. If instead the files in the directory are to be ignored, that's a different question altogether.
  • ofavre
    ofavre over 9 years
    It's unlikely that this tweak will work with any other tool. Like stated in the warning and the edit, I discourage using it unless in a quite restricted case.
  • Roman
    Roman over 9 years
    You missed one thought - whats the reason for keeping and empty folder (e.g. /logs, /tmp, /uploads)? Yes - its to keep the folder empty. :) So if you want to keep a folder empty, you have to ignore the files inside it.
  • Asclepius
    Asclepius over 9 years
    It was suggested that git clean -nd | sed s/'^Would remove '// | xargs -I{} touch "{}.keep" will do this in all untracked empty directories.
  • shrekuu
    shrekuu over 9 years
    This what I need. Thanks. I just need a 'log' folder to put logging file, but I do not want to add any of these log files in git. PHP throws an error if the directory does not exist.
  • eonil
    eonil over 9 years
    I recommend to put a file named PLACEHOLDER instead of README for subdirectories.
  • Dave
    Dave over 9 years
    In this case a README or ABOUT file would be just as good or better. Leaving a note for the next guy, just like we all used to do it before URLs.
  • pedorro
    pedorro over 9 years
    Unfortunately, this results in a non-empty directory, it has a single hidden file.
  • blueFast
    blueFast about 9 years
    @RomanAllenstein: not necessarily. It could be that you create a repo with a given structure which can become populated later. Those files will be added to the repo as soon as they are created, and it will be annoying to start deleting or editing .gitignore files (and dangerous, because probably you do not even realize that they are not being tracked: git is ignoring them)
  • blueFast
    blueFast about 9 years
    Another usecase: I keep my /etc in version control. Some directories there are empty, but I want to track them in my repo (they must be there, otherwise some tools will simply not work). But I do not want to ignore the files there: as soon as I apt-get update ; apt-get upgrade new files can appear, which I for sure do not want to ignore.
  • clacke
    clacke about 9 years
    /bin/sh cultural assumption!* If "here" is csh and the variable noclobber is set, you will indeed get file: File exists. If someone says "I get this", don't assume they're an idiot and reply "No you don't". * c2.com/cgi/wiki?AmericanCulturalAssumption
  • clacke
    clacke about 9 years
    I know you posted this as an example of a bad argument, but I appreciate the link because it's actually a well-reasoned argument against tracking directories. ;-)
  • Lightbulb1
    Lightbulb1 about 9 years
    This is the route i finally decided on I was thinking of this when i saw people saying about .gitkeep for my case this is the better solution in my opinion. Its for keeping folders like lib and obj which is apart of my build process. OK a readme is overkill for other developers but for people just starting, I certainly would of benefited from having readmes back when i started. I think it depends on the project and the way its going to be used.
  • zerweck
    zerweck almost 9 years
    Since OS X creates a .DS_Store file in almost every directoy, this does not work there. The only (DANGEROUS!) workaround i found, was to delete all the .DS_Store files first via find . -name .DS_Store -exec rm {} \; and then use the preferred variant from this answer. Be sure to only execute this in the correct folder!
  • Jochen Schultz
    Jochen Schultz almost 9 years
    I can't see how this can be a waste of time. When your TEMPLATEPATH is obviously dynamic you can't use the .gitkeep solution. And even with a nondynamic folder structure you should add some more stuff instead of removing the very good solution of checking directories e.g. check for permissions and chmod the files. Adding a way to mark directories inside a global .gitignore would be perfect for me. Something like #keep /path/to/dir
  • Casey
    Casey almost 9 years
    And of course this is why messing with the git internals is contraindicated.
  • Paul Draper
    Paul Draper over 8 years
    @GreenAsJade, but when you add files later, you probably want to delete this placeholder .gitignore. And when you do that, the files are tracked. I'm not saying your point is wrong, just a real edge case.
  • Quolonel Questions
    Quolonel Questions over 8 years
    What does that have to do with Ruby on Rails?
  • still_dreaming_1
    still_dreaming_1 over 8 years
    Typically, the point of a directory is to contain files. If you want to add an empty directory to a repository, it will probably eventually contain files, at least in one of the repositories or releases out there. But you probably don't want those files to be part of the repository, otherwise it would likely already contain one or more of those files or you could just wait until you add them. So for the primary case where you actually want an "empty" directory in the repository, this solution is perfect.
  • atelcikti1
    atelcikti1 over 8 years
    Doesn't work if you're writing a unit test that should test code on an empty directory...
  • vpalmu
    vpalmu about 8 years
    I got here looking at a case where the build fell down if the directory doesn't exist and by default it is empty, but it doesn't need to be empty. Creating a .gitignore does the right thing.
  • danielrvt
    danielrvt almost 8 years
    Don't like this solution, it is tough to guess what this file does. Also, if you are generating files in your dev environment (like logs or images, etc.), this isn`t keeping those file from being versioned and making their way into production, which is not nice.
  • Seldom 'Where's Monica' Needy
    Seldom 'Where's Monica' Needy almost 8 years
    @clacke If someone decides to use a different shell than everyone else, they should state that expressly if they are encountering problems. Unlike with nationality, everyone has their free choice of shell.
  • Shital Shah
    Shital Shah almost 8 years
    There is no need to put anything in .gitignore. Just check-in empty .gitignore. Many folders may get populated in future and the method in this answer will never add any files. So I would use this answer's approach very cautiously.
  • clacke
    clacke almost 8 years
    @SeldomNeedy Maybe they are looking for help because they don't even know they are using a different shell than everybody else.
  • EntangledLoops
    EntangledLoops over 7 years
    Windows doesn't like files without names and requires special magic to accomplish this (aka a bash-like terminal app or equivalent).
  • EntangledLoops
    EntangledLoops over 7 years
    @GreenAsJade Actually, it doesn't do precisely what was asked for, as the directory isn't empty. And as other users suggested, it's a bad idea to prefix with "git" as it's misleading---this is not a "git aware" strategy.
  • Johnny
    Johnny over 7 years
    If you edit your answer to replace .gitkeep with any other non git-prefixed file name you get my upvote, I think this one is the best and most informative answer. Reason: I think ".git*" should be reserved for git prescribed files, while this is just a mere placeholder. My first guess when I saw that is that for example a ".gitkeep" file would be auto-ignored (that would be a nice feature) but that is not the case, right?
  • skullkid
    skullkid over 7 years
    Although adding the .gitignore does guarantee that the directory remains empty, as soon as you have a file to commit you can just delete the .gitignore. It's simple and easy enough.
  • Mig82
    Mig82 over 7 years
    Does anyone know a way to do this in Windows from the command line? I've seen some solutions here in Ruby and Python, but I'd like a barebones solution if it can be managed.
  • Ponkadoodle
    Ponkadoodle over 7 years
    If you have a .htaccess file that's under version control, then you already have the directory containing it under version control. Thus, the problem is already solved - the .gitignore file becomes irrelevant.
  • akhan
    akhan about 7 years
    @zerweck You should have .DS_Store in .gitignore. See this link on to set it up.
  • zerweck
    zerweck about 7 years
    @akhan Adding something to .gitignore has no influence on the -empty flag of the find command. My comment is about removing the .DS_Store files in a directory tree, so the -empty flag can be applied.
  • Radon Rosborough
    Radon Rosborough almost 7 years
  • Mig82
    Mig82 almost 7 years
    @szablica I don't think it's confusing at all. In fact I think it's very intuitive to call it .gitkeep. Calling it .gitignore is what sounds contradictory to me. So this is just a matter of personal taste.
  • Patrick M
    Patrick M over 6 years
    This answer seems to be inconsistent, since in the next post on the referenced thread, Linus Torvald says he expects that they will need to add directory tracking: markmail.org/message/libip4vpvvxhyqbl . In fact, he says he "would welcome patches that [add support for tracking empty directories]"
  • user2334883
    user2334883 over 6 years
    Patrick, he also uses the word "idiotic" there. I suspect his wording adresses the people here in this thread and so I assume he will not implement something "idiotic" into Git by himself.
  • David
    David over 6 years
    @Wallacoloo Related to the question you're right, nevertheless the file is useful, I'll use it for an upload-directory like that where files shall be protected by .htaccess. Contrary to Romans explanation the .htaccess-file will be committed as it's excluded by the ignore-rule. [old thread, I know]
  • Tanasis
    Tanasis over 6 years
    Elegant: the .keep file together with the commit message shows the intent of "keeping" the project structure. Adding Readme or Abouts I think will cause more confusion...
  • Cranio
    Cranio over 6 years
    @Santosh You could edit my post and be useful to the community instead of childishly bragging against a non-native speaker and uselessly polluting the comments, which is [IN]consistent with average intelligent behaviour. That's why edits are for, btw. Thanks for the free lesson anyway, duly appreciated :)
  • Thomas Weller
    Thomas Weller over 6 years
    @Amala: what are you referring to, when you say "below". The order of answers is changing...
  • danilo
    danilo about 6 years
    Thanks @Cranio for your excellent post, upvote. You can improve it by adding an option to just use .keep
  • tjfwalker
    tjfwalker about 6 years
    @szablica, every thing is confusing to someone ¯\_(ツ)_/¯
  • rogerdpack
    rogerdpack almost 6 years
    This actually works but I think confuses the heck out of IntelliJ... :|
  • Ketan Yekale
    Ketan Yekale over 5 years
    This is the best solution to this to the question as by design git doesn't allow you to store empty folders and .gitignore can make sure that it stays empty.
  • Blackbam
    Blackbam over 5 years
    @GreenAsJade Yes good point, but sometimes this is exactly what is required. In My case this is for WordPress Plugins' cache folders (which need to exist as errors are thrown otherwise but you never ever want to commit any cache file).
  • BitTickler
    BitTickler over 5 years
    I wonder why people have such a hard time to understand why one wants to add "empty" folders to git. You have to start somewhere, right? So, usually you start with your projects folder structure and - alas - at the start of the project there is nothing there yet. Once your project repo is done, team workers can clone and start working on the SAME structure.
  • mit
    mit over 5 years
    The reasons to use .keep are very good. A good balance between simple and less confusing. See the other answer stackoverflow.com/a/21422128/832230 If you have adopted the .gitkeep habit, run this over your repository: git mv .gitkeep .keep
  • mit
    mit over 5 years
    The reasons to use .keep over .gitkeep are very good. A good balance between simple and less confusing. Namespaces seem important and it should not be broken that .git.... looks like git's own file namespace. Additional documentation could go the projects global readme or coding style guides or a local readme. If you have adopted the .gitkeep habit, simply find the files and rename them: git mv .gitkeep .keep. Embrace change! The .gitignore solution recommended by the git documentation seems a hacky workaround and over complicated.
  • HelloGoodbye
    HelloGoodbye over 5 years
    @GreenAsJade What? It is definitely not considered an an empty directory for all intents and purposes! For example, a Python script will see this file if it looks for files in the folder, why the presence of a .gitignore file may make a significant difference.
  • HelloGoodbye
    HelloGoodbye over 5 years
    On the other hand, the problem is very Git-dependent, as you wouldn't have this problem unless if it was because of the fact that you version control your project.
  • ntninja
    ntninja over 4 years
    You can actually add empty directories to a GIT repo without confusing the hack out of GIT + associated tools, by “technically playing by the rules” while doing so! The solution of course still involves submodules, but they only serve as a placeholder to make stuff look legit to tooling.
  • ntninja
    ntninja over 4 years
    I've created a better solution based on this that doesn't have these drawbacks: stackoverflow.com/a/58543445/277882
  • ntninja
    ntninja over 4 years
    I've created a better solution based on this that doesn't have these drawbacks: stackoverflow.com/a/58543445/277882
  • FiringSquadWitness
    FiringSquadWitness over 4 years
    Nice.‌‌ ༼ ͡☉ ͜ʖ ͡☉ ༽
  • David K
    David K over 4 years
    Usually use .empty or .keep
  • Victor Yarema
    Victor Yarema over 4 years
    .emptydir can be less confusing than .gitkeep.
  • Szczepan Hołyszewski
    Szczepan Hołyszewski over 4 years
    Almost empty ≠ empty. Question author asked about empty directories, and you should have assumed that they had a very good reason for that.
  • Szczepan Hołyszewski
    Szczepan Hołyszewski over 4 years
    This should be the accepted answer to the question as it was asked. An impossibility result is a valuable result. It saves the time that would be wasted looking for a solution that does not exist.
  • Mohamad Shiralizadeh
    Mohamad Shiralizadeh about 4 years
    you can create this file in windows using this command echo>.gitkeep
  • Héctor
    Héctor almost 4 years
    Would you not need to replace !.gitignore with !/.gitignore to make sure other .gitignore files that you could eventually find in the ignored folder subtree are also ignored?
  • Owen S.
    Owen S. almost 4 years
    Whether you like the naming convention or not, this solution is one I've seen in more than one shop.
  • valiD
    valiD almost 4 years
    This one is the best method. The only argument I see around here is that .gitkeep in not documented. Guys, just use an empty .gitignore and add it. Your folder will be tracked and also delete proof in a sense that git status will show the missing tracked .gitignore file in case the folder does not exist anymore.
  • Brook Jordan
    Brook Jordan over 3 years
    They were asking for an empty directory, not an empty repository. Also, you’ve assumed they are using GitHub when they only talked about git.
  • grenix
    grenix almost 3 years
    what about find . -type d -empty -exec echo mkdir -p {} \; > make_empty_dirs.sh and then commit make_empty_dirs.sh ?
  • GDB
    GDB almost 3 years
    I didn't see an example of this though the README solution proposed by @JohnMee is suggested in the comments above. This works: # Ignore everything in this directory * # Except these files !.gitignore !README.md
  • David
    David almost 3 years
  • dmgig
    dmgig about 2 years
    This is great. I used to do the .gitkeep file and a similar few lines in the main .gitignore, but I prefer this. Just seems more git-y. Thanks.
  • Gabriel Staples
    Gabriel Staples about 2 years
    Upvoted. For anyone who wants to learn more about the nuances of UNignoring files or folders, see my answer here: How to UNignore some select contents (files or folders) within an ignored folder
  • Gregor y
    Gregor y almost 2 years
    for what it's worth, a windows powershell one-liner could be ls -r -dir | ?{$_.getFileSystemInfos().Count -eq 0} | %{ni -p $_.FullName -n .gitkeep}
  • Gregor y
    Gregor y almost 2 years
    also, ni -p $_.FullName -n .gitignore -v "*`r`n!.gitignore"