959

I want to check in a blank folder to my Git repository. Effectively, I need to ignore all of the files and folders within the folder, but not the folder itself. How can I do this? What should I put in my .gitignore file?

For those wondering why I would want to do this, I have an "upload" directory in my repository. I want to commit the blank directory, but without all the contents.

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
Aron Woost
  • 17,389
  • 13
  • 42
  • 49
  • 7
    I had exactly the same thing. Just added a README file to that explaining that this is the upload directory and git understood it :) – Evgeny Nov 22 '10 at 20:52
  • 1
    You've pretty much answered your own question: put a .gitignore which says "ignore everything" into that folder; this is mipadi's answer. – Cascabel Nov 22 '10 at 21:13
  • 1
    This is useful to put empty build folder in git. No content inside build folder needs to be checked in – Vishnudev K Mar 13 '15 at 10:42
  • 1
    I still can't believe Git has no way to do this properly. SVN can do it. – BoffinBrain Aug 23 '17 at 15:50
  • I know this has been marked as a duplicate but just for the sake of completeness and for those reading this in the future: [Git does not version folders](https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F). The typical workaround is to include a `.gitkeep` or `.keep` file in the folder you want to version. – Mig82 Feb 12 '19 at 09:28
  • I've developed a [helper tool for this which you can find in here](https://github.com/mig82/py-gitkeep). You can install it running `pip3 install gitkeep2` and then run `gitkeep --recursive path/to/foo` to keep folder `foo` and all its subfolders. It also allows you to comment those `.gitkeep` files and remove them recursively or not. – Mig82 Feb 12 '19 at 09:32

2 Answers2

1552

Put this .gitignore into the folder, then git add .gitignore.

*
*/
!.gitignore

The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.

Edit: May be obvious but also add */ to the .gitignore to also ignore subfolders.

Qrzysio
  • 1,082
  • 2
  • 12
  • 22
Trianam
  • 15,643
  • 2
  • 14
  • 2
910

You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

# exclude everything
somefolder/*

# exception to the rule
!somefolder/.gitkeep 

Commit your .gitignore and .gitkeep files and this should resolve your issue.

Qrzysio
  • 1,082
  • 2
  • 12
  • 22
kubi
  • 46,368
  • 19
  • 92
  • 118