[Git] 空のディレクトリを追加する

実行例

Gitはその仕様上、空のディレクトリを登録してくれません。

$ mkdir empty
$ ls
empty

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

git addしても取り合ってくれません。

$ git add empty
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

そこで空のディレクトリを登録するためには、何か適当なファイルを設置する必要があるのです。Git界隈では慣習として .gitkeep という名前の空ファイルを用意することが多いようですが、ファイル名は何でも良いです。

$ touch empty/.gitkeep
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        empty/

nothing added to commit but untracked files present (use "git add" to track)

ファイルを追加した時点でディレクトリではないやろというツッコミは、大人はしちゃダメですw

参考