編集済みの.gitignoreを簡単に得る方法

gitのリポジトリを作ったとき、.gitignoreをゼロから書くのが面倒だと感じたことはありませんか? プログラミング言語やフレームワークなどに応じた内容が予め記載された.gitignoreファイルを得る方法にについて纏めてみます。

github/gitignore

githubが公開しているgithub/gitignoreというリポジトリには、様々な言語やフレームワーク、開発環境に応じた.gitignoreファイルがコレクションされています。

例えば、rubyのプロジェクトのためには、Ruby.gitignoreというファイルがあり、この中身は、

*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

となっていて、通常、除外するであろう内容が予め定義されています。

また、Railsを使ったプロジェクトためのRails.gitignoreというファイルがあり、この中身は、

*.rbc
capybara-*.html
.rspec
/log
/tmp
/db/*.sqlite3
/db/*.sqlite3-journal
/public/system
/coverage/
/spec/tmp
**.orig
rerun.txt
pickle-email-*.html

# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb

# Only include if you have production secrets in this file, which is no longer a Rails default
# config/secrets.yml

# dotenv
# TODO Comment out this rule if environment variables can be committed
.env

## Environment normalization:
/.bundle
/vendor/bundle

# these should all be checked in to normalize the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# if using bower-rails ignore default bower_components path bower.json files
/vendor/assets/bower_components
*.bowerrc
bower.json

# Ignore pow environment settings
.powenv

# Ignore Byebug command history file.
.byebug_history

となっていて、プログラミング言語だけではなく、様々なフレームワークを使った開発でもそのまま使える.gitignoreが用意されていることがわかります。

したがって、このリポジトリから適切なファイルをダウンロードして利用すれば、ゼロから作成しなくても済みます。

gibo

もう1つは、giboというツールを使う、という方法があります。

giboは、Shellで書かれたコマンドラインツールで、オープンソースとして公開されています。 このツールを使うと、指定した言語やフレームワークに対応した.gitignoreを、上述のgithub/gitignoreから取得し、出力してくれます。

インストール

OSXでHomebrewを使っている場合は、brew install giboで簡単にインストールできます。

その他の環境(LinuxやWindowsなど)へのインストール方法は、giboのREADMEに詳しく書かれています。

使ってみる

試しにgiboを使ってrubyのための.gitignoreを出力してみます。

ターミナルで、

gibo Ruby

を実行すると、

### https://raw.github.com/github/gitignore/d74a7f317c24e8f2c8adca3e7d8484b94f738c9a/Ruby.gitignore

*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

が出力されました。

これはgithub/gitignoreのRuby.gitignoreと同じであることがわかります。

続いて、Railsのための.gitignoreを出力してみます。

gibo Rails

を実行すると、

### https://raw.github.com/github/gitignore/d74a7f317c24e8f2c8adca3e7d8484b94f738c9a/rails.gitignore

*.rbc
capybara-*.html
.rspec
/log
/tmp
/db/*.sqlite3
/db/*.sqlite3-journal
/public/system
/coverage/
/spec/tmp
**.orig
rerun.txt
pickle-email-*.html

# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb

# Only include if you have production secrets in this file, which is no longer a Rails default
# config/secrets.yml

# dotenv
# TODO Comment out this rule if environment variables can be committed
.env

## Environment normalization:
/.bundle
/vendor/bundle

# these should all be checked in to normalize the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# if using bower-rails ignore default bower_components path bower.json files
/vendor/assets/bower_components
*.bowerrc
bower.json

# Ignore pow environment settings
.powenv

# Ignore Byebug command history file.
.byebug_history

これもgithub/gitignoreのRails.gitignoreと同じであることがわかります。

というわけで、giboを使うと、更に簡単に.gitignoreが手に入りそうです。

ちなみに、対応する言語やフレームワークなどを調べるには、-lスイッチを指定して実行します。

gibo -l
=== Languages ===

Actionscript		DM			Jboss			Packer			Stella
Ada			Drupal			Jekyll			Perl			SugarCRM
Agda			Eagle			Joomla			Phalcon			Swift
Android			Elisp			Julia			PlayFramework		Symfony
AppceleratorTitanium	Elixir			KiCad			Plone			SymphonyCMS
AppEngine		Elm			Kohana			Prestashop		Terraform
ArchLinuxPackages	EPiServer		LabVIEW			Processing		TeX
Autotools		Erlang			Laravel			Python			Textpattern
C++			ExpressionEngine	Leiningen		Qooxdoo			TurboGears2
C			ExtJs			LemonStand		Qt			Typo3
CakePHP			Fancy			Lilypond		R			Umbraco
CFWheels		Finale			Lithium			Rails			Unity
ChefCookbook		ForceDotCom		Lua			RhodesRhomobile		UnrealEngine
Clojure			Fortran			Magento			ROS			VisualStudio
CMake			FuelPHP			Maven			Ruby			VVVV
CodeIgniter		Gcov			Mercury			Rust			Waf
CommonLisp		GitBook			MetaProgrammingSystem	Sass			WordPress
Composer		Go			Nanoc			Scala			Xojo
Concrete5		Gradle			Nim			Scheme			Yeoman
Coq			Grails			Node			SCons			Yii
CraftCMS		GWT			Objective-C		Scrivener		ZendFramework
CUDA			Haskell			OCaml			Sdcc			Zephir
D			Idris			Opa			SeamGen
Dart			IGORPro			OpenCart		SketchUp
Delphi			Java			OracleForms		Smalltalk

=== Global ===

Anjuta			Eclipse			LibreOffice		NotepadPP		Vagrant
Ansible			EiffelStudio		Linux			Otto			Vim
Archives		Emacs			LyX			Redcar			VirtualEnv
Bazaar			Ensime			macOS			Redis			VisualStudioCode
BricxCC			Espresso		Matlab			SBT			WebMethods
Calabash		FlexBuilder		Mercurial		SlickEdit		Windows
Cloud9			GPG			MicrosoftOffice		SublimeText		Xcode
CodeKit			JDeveloper		ModelSim		SVN			XilinxISE
CVS			JetBrains		Momentics		SynopsysVCS
DartEditor		Kate			MonoDevelop		Tags
Dreamweaver		KDevelop4		NetBeans		TextMate
Dropbox			Lazarus			Ninja			TortoiseGit

まとめ

.gitignoreが必要となった際は、github/gitignoreから拝借するか、giboでバシッと作ってしまえば、ちょっぴり幸せになれそうですね。