How to track directories but not their files with Git? How to track directories but not their files with Git? git git

How to track directories but not their files with Git?


Git doesn't track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignore file looks something like this:

upload/*

You can do this:

$ touch upload/.placeholder$ git add -f upload/.placeholder

If you forget the -f you'll see:

$ git add upload/.placeholderThe following paths are ignored by one of your .gitignore files:uploadUse -f if you really want to add them.fatal: no files added

Then when you do git status you'll see:

# On branch master## Initial commit## Changes to be committed:#   (use "git rm --cached ..." to unstage)##   new file:   upload/.placeholder#

Obviously you can then do:

$ touch upload/images/.placeholder$ git add -f upload/images/.placeholder


I wrote about this here.

Add a .gitignore within the directory.


Best answerd I've found is to include a .gitignore file in your upload folder with this content

# Ignore everything in this directory*# Except this file!.gitignore

Here you have How can I add an empty directory to a Git repository?