0

I'm trying to commit the following directory structure to git:

web
    /uploads
            /avatars
            /video

I put .gitkeep into /web/uploads/avatars/ and /web/uploads/video/ and added the following lines to .gitignore in the root of the repo:

/web/uploads/
!/web/uploads/avatars/.gitkeep
!/web/uploads/video/.gitkeep

But git status does not show any new files. Invoking git add web/uploads/avatars/.gitkeep outputs the following message:

The following paths are ignored by one of your .gitignore files:
web/uploads/avatars/.gitkeep
Use -f if you really want to add them.
fatal: no files added

How can I commit an empty directory structure into git repository? P.S. git version is git version 1.8.4.msysgit.0

Max Romanovsky
  • 2,824
  • 5
  • 30
  • 37
  • This is (because you want the lowest-level directory to be empty and the problem then simply floats up to each next level) a duplicate of [How do I add an empty directory to a git repository](http://stackoverflow.com/q/115983/1256452) (or, perhaps, of [Make .gitignore ignore everything except a few files](http://stackoverflow.com/q/987142/1256452)). – torek Oct 25 '13 at 10:06

3 Answers3

1

If you exclude some directory, then everything under it will always be excluded, even if you put unignore something under it later. To unignore certain files or directories you have to unignore every parent directory of anything that you want to unignore. So your .gitconfig must be

!web/uploads/

web/uploads/*
!web/uploads/avatars/
!web/uploads/video/

web/uploads/avatars/*
web/uploads/video/*

!web/uploads/*/.gitkeep

But you should remember that .gitkeep is not a git feature, it just an agreement, simply it is a hidden file, so the name can be any

cooperok
  • 4,249
  • 3
  • 30
  • 48
0

Your .gitignore file ignored directory /web/uploads itself therefore its subdirectories and files will be ignored automatically regardless the following negated pattern.

If you want to add /web/uploads/ but ignore files in it, the .gitignore can be like:

/web/uploads/*
# Next line tell git to add subdirectories back
!/web/uploads/**/
!/web/uploads/avatars/.gitkeep
!/web/uploads/video/.gitkeep

Or if you just want to add the directory, no matter there is file or no, then you don't need to touch .gitignore, just add the .gitkeep files, everything will work well.

dyng
  • 2,854
  • 21
  • 24
0

If you ignore a directory, git doesn't check its subdirectories and files.

So, git doesn't check following lines.

!/web/uploads/avatars/.gitkeep
!/web/uploads/video/.gitkeep

If you want git to check subdirectories and files, use asterisk and ignore all files (not directory).

For example:

/web/uploads/*/*
krsteeve
  • 1,794
  • 4
  • 19
  • 29
ton
  • 1,524
  • 13
  • 10