LoginSignup
4
5

More than 5 years have passed since last update.

不要な .gitkeep を消す

Posted at

概要

不要になった .gitkeep を一括して削除するgitのサブコマンドを作った。

背景

gitは空ディレクトリを管理できないので、ディレクトリを存在させるために、空のディレクトリには .gitkeep のような名前のファイルを置いておく習慣がある。

最初は空だったディレクトリも、のちに管理するファイルが追加されて空ではなくなった場合、そのディレクトリ内の .gitkeep は不要になるので、消してしまいたい。

実装

git-rm-keep
#!/bin/sh

for keep in $(git ls-files | grep -F ${GIT_KEEPFILE:-.gitkeep}); do
  [ $(git ls-files $(dirname $keep) | wc -l) -gt 1 ] && git rm $keep
done

上記のスクリプトに実行ビットを付けて PATH のどこかに置いておく。

git は、 サブコマンドが見つからない場合に、 PATH の通ったところに置かれている git-サブコマンド名 という名前のコマンドを実行してくれるので、

$ git rm-keep

を実行すると、空ではないディレクトリに置かれている .gitkeep を git rm する。

.gitkeep 以外の名前の場合は、

$ GIT_KEEPFILE=.keep git rm-keep

のようにする。

補足

ほぼ同じスクリプトを書いている人がいた…

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5