How to .gitignore all files/folder in a folder, but not the folder itself? [duplicate]

Question

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.

Answer

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.

Throw away local commits in Git

How do I clear my local working directory in Git? [duplicate]