1099

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 - on strike
  • 239,200
  • 50
  • 490
  • 574
Aron Woost
  • 19,268
  • 13
  • 43
  • 51
  • 8
    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
  • 2
    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

1719

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.

Profilüfter
  • 81
  • 2
  • 12
Trianam
  • 17,313
  • 2
  • 15
  • 2
  • 98
    Ignoring directories `*/` is not necessary because `*` also covers directories. – wst Feb 01 '18 at 08:11
  • 6
    [NB](https://git-scm.com/docs/gitignore#_pattern_format) «It is not possible to re-include a file if a parent directory of that file is excluded.» – Timofey Gorshkov Jan 17 '19 at 10:58
  • 4
    Workaround for @TimofeyGorshkov's point above: `* ; !dir/ ; dir/* ; !dir/reincludefile` – OJFord May 18 '19 at 22:34
  • 5
    One should note, the second `.gitignore` does not need to named that. Any `filename` will do as long as the file's contents include `!filename`. I mention this because we thought having a `.gitignore` file which functions to keep a directory seemed misleading and unclear. – Derek Henderson Apr 07 '20 at 13:18
  • 4
    This should be the accepted solution. – slick1537 May 07 '20 at 13:18
  • Really, the best solution so far. – Artur Barseghyan Jun 23 '21 at 13:39
  • Why is this 'the best solution'... `.gitkeep` was literally designed for this? – ortonomy Oct 18 '21 at 05:06
  • `*` ignores all files and directories, sub-files and sub-directories, ...etc. I think this will work the same way `*; !*/`, which ignores all files and includes all directories which will be ignored as well because there are no non-ignored files in them. After that you can include any file you want `!dir/reincludefile` – Mohammed Samir Aug 10 '22 at 09:44
1012

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,147
  • 3
  • 12
  • 25
kubi
  • 48,104
  • 19
  • 94
  • 118
  • 2
    isnt there a `--bare` option? – sdot257 Nov 22 '10 at 20:56
  • 70
    The best way is as listed above by Trianam. Include a .gitignore file at the dir. This .gitignore should ignore all files inside that dir (*), except itself (!.gitignore). – Leandro Alves Jul 30 '12 at 17:24
  • Only commit and empty folder dose not solve the issue of be sure files in this folder will never be traked. Better to use a .gitignore file as @Trianam showed below. – gabrielem Aug 15 '15 at 12:41
  • 1
    @LeandroAlves creating a.gitignore file inside of each directory is bad practice at all... – Yousha Aleayoub Sep 17 '20 at 10:25
  • Is there a reason why putting `somefolder/` (without a star) in .gitignore would override later negation and would not not include .gitkeep? – ruslaniv Feb 23 '21 at 18:33
  • 11
    this does not work – Sunil Garg Nov 26 '21 at 07:36
  • @kubi what is the difference betw: `.gitignore` vs `.gitkeep` ? I'm aware that a `.gitignore` file explicity instructs `git` to avoid staging certain files/folders. cc: @LeandroAlves – Sumax Jun 29 '22 at 13:34
  • this was not working for me until I added a forward slash in front of the folder name like this `/content/folder/*` I also had to do this, but for some users it may not be necessary https://stackoverflow.com/a/1274447/11439685 – Isaac Tait Sep 12 '22 at 13:54
  • works for me. And @Sumax .gitkeep is a standard file name meant to prevent the deletion of purposely empty folders – JohnP2 Nov 15 '22 at 17:26
  • how to ignore specific folder that can have any path on any folder? For example, user/cache, project/folder/system/cache.. I need to ignore this "cache" folder. – Curious Developer Mar 06 '23 at 12:50
  • @CuriousDeveloper to ignore content and subdirectories: `**/cache/*` [Source](https://stackoverflow.com/questions/2545602/how-to-git-ignore-subfolders-subdirectories#:~:text=To%20exclude%20content%20and%20subdirectories%3A) – Sumax Jun 12 '23 at 08:41