Joe Li
Joe Li

Reputation: 155

Why does "git rm dir/file" delete `dir` as well, when `file` is the only file in `dir`?

Start with an empty directory.

git init
mkdir dir
touch dir/file1
git add .
git commit -m "message"
git rm dir/file1

ls shows that nothing is left. But I'm expecting dir, which is the result when /bin/rm is used instead of git rm. What happened?

Upvotes: 1

Views: 33

Answers (1)

Cyrille Pontvieux
Cyrille Pontvieux

Reputation: 2471

Because git monitor contents only, and contents can only reside in files.

So empty directory cannot be monitor by git, hence it's deleted.

To keep a directory, one usually create a dummy file it in. Usually the file is named .gitkeep or .gitignore. You can leave the file empty.

Upvotes: 1

Related Questions