How do I keep an empty directory? .gitkeep or .gitignore?

Normal Git add and checkout workflows preserve directories through the files inside them. Choose one placeholder approach for each directory: .gitkeep for an initially empty directory, or .gitignore when future generated contents should stay ignored.

1. Keep a placeholder for future content

Create uploads and an empty uploads/.gitkeep file in your editor, then stage that specific file:

git add -- uploads/.gitkeep

Verify the index lists uploads/.gitkeep. The name .gitkeep is a convention, with no special Git behavior; a README explaining the directory’s purpose also works. It does not automatically ignore files added later.

git ls-files -- uploads/

2. Keep the directory but ignore generated contents

For this alternative, create uploads/.gitignore with the two lines below instead of choosing the .gitkeep approach. * ignores contents and !.gitignore keeps the rule file eligible for tracking.

The rule file itself is the tracked placeholder, so the directory contains that file. This approach deliberately ignores future files until the rules are adjusted.

*
!.gitignore

Why does ! fail to keep a file inside an ignored directory?

3. Verify the chosen approach

For the .gitignore approach, stage its rule file:

git add -- uploads/.gitignore

Create uploads/example.bin and check that its rule matches:

git check-ignore -v -- uploads/example.bin

List the index entries; uploads/.gitignore should appear and example.bin should not. If a parent rule excludes uploads itself, fix the parent rule first so Git can read the nested .gitignore.

git ls-files -- uploads/

How do I ignore folders? folder/ versus /folder/

For exact behavior and further details, consult the official references: Git documentation · git add documentation