手軽に gitignore をいい感じにする。

twitter でgitignore についてちょっと話題に上がってたので、自分のgitignore の管理方法についてドヤしたい。

正直、.gitignore を書くはが面倒なのはすごくわかる。 とはいえ、.gitignore を書かなくて .DS_Store とか あげるのもだるい。

そこで紹介したいのは、gitignore.io を利用した .gitignore のテンプレート生成 コマンドを使う方法です。

前提として、curl コマンドを使うので、事前にインストールしておいてください。

作り方

git のサブコマンドとして git ignore を使える様にします。

以下のコマンドをターミナルから実行します。

// サブコマンドを作る
% echo 'curl -L -s https://www.gitignore.io/api/$@' | tee /usr/local/bin/git-ignore

// 実行権限追加
% chmod +x /usr/local/bin/git-ignore

これで git ignore サブコマンドが使える様になったはずです。

使い方: mac のgitignore を作る

たとえば、mac についての gitignore を作りたい時は、次の様に実行します。

% git ignore macos

# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/macos

こういう風に、mac で頻出するであろうファイルについてのgitignore が生成されるので、.gitignore にリダイレクトすればよかろう。

% git ignore macos >> .gitignore

使い方: 複数のgitignore を同時に作る。

maclinuxvim に関してのgitignore を同時に作りたい時、ありませんか? そういう時は、コンマ区切りで指定することで、同時にgitignore を作ることができます。

% git ignore macos,linux,vim
(アウトプットは各自確認してみてください)

注意点

このコマンドは、gitignore.io http://gitignore.ioapi に完全に依存しています。

OSS で開発されていますので、アクセスログなどを取得するのを気にする方は、自前でビルドして使うこともできます。 https://github.com/toptal/gitignore.io

参照

docs.gitignore.io