Visual Studio Code で 使うために PortableGit を インストールしました. その際に 全体 gitignore を 設定したのですが、よくよく調べると、もっとよい設定方法がったので再設定します.
作業環境
- Windows 10 64bit
- PortableGit 64bit
- Git Hub
github/gitignore リポジトリ
GitHub 社 が 公開している github/gitignore というリポジトリ https://github.com/github/gitignore というのがあります.
様々なプログラム言語やフレームワークなどに応じた gitignore の テンプレートを提供してくれています. GitHub.com で リポジトリを作成する際や、作成した後から Web UI で gitignore を 追加する機能がありますが、その gitignore ファイルは このリポジトリから作られているとのことです.
アプリケーションを作る際などに、いつも参照させていただくのですが、よくよく見ていると Global というディレクトリがあり、こちらは OS や エディタなどのテンプレートが入っているとのことです. 知らなかった…
ちゃんと説明 Globally Useful gitignores - gitignore/Global at master · github/gitignore を 読まないとですね. ということで、こちらをもとに グローバル の gitignore を 再設定 します.
まず Windows.gitignore を 参照してみます. 2017年9月現在、以下の内容でした. 前回設定した Thumbs.db
だけとは大違いですね. 勉強になります.
1 | # Windows thumbnail cache files |
2 | Thumbs.db |
3 | ehthumbs.db |
4 | ehthumbs_vista.db |
5 | |
6 | # Dump file |
7 | *.stackdump |
8 | |
9 | # Folder config file |
10 | Desktop.ini |
11 | |
12 | # Recycle Bin used on file shares |
13 | $RECYCLE.BIN/ |
14 | |
15 | # Windows Installer files |
16 | *.cab |
17 | *.msi |
18 | *.msm |
19 | *.msp |
20 | |
21 | # Windows shortcuts |
22 | *.lnk |
Visual Studio Code の 設定もありました. .vscode/
以下の Visual Studio Code 設定ファイル以外を ignore ですが、これでいいのかな?ちょっと意図を知りたいかも.
1 | .vscode/* |
2 | !.vscode/settings.json |
3 | !.vscode/tasks.json |
4 | !.vscode/launch.json |
5 | !.vscode/extensions.json |
Eclipse の 設定もあります. Eclipse で 関しているわけではないのですが、Eclipse エディタで編集したい部分があるなどで、Eclipse プロジェクトにしている場合があります. その場合は .project
や .settings
などの Eclipse 関連ファイルはコミットしたくないので、グローバル gitignore するのもよさそうです.
1 | .metadata |
2 | bin/ |
3 | tmp/ |
4 | *.tmp |
5 | *.bak |
6 | *.swp |
7 | *~.nib |
8 | local.properties |
9 | .settings/ |
10 | .loadpath |
11 | .recommenders |
12 | |
13 | # External tool builders |
14 | .externalToolBuilders/ |
15 | |
16 | # Locally stored "Eclipse launch configurations" |
17 | *.launch |
18 | |
19 | # PyDev specific (Python IDE for Eclipse) |
20 | *.pydevproject |
21 | |
22 | # CDT-specific (C/C++ Development Tooling) |
23 | .cproject |
24 | |
25 | # Java annotation processor (APT) |
26 | .factorypath |
27 | |
28 | # PDT-specific (PHP Development Tools) |
29 | .buildpath |
30 | |
31 | # sbteclipse plugin |
32 | .target |
33 | |
34 | # Tern plugin |
35 | .tern-project |
36 | |
37 | # TeXlipse plugin |
38 | .texlipse |
39 | |
40 | # STS (Spring Tool Suite) |
41 | .springBeans |
42 | |
43 | # Code Recommenders |
44 | .recommenders/ |
45 | |
46 | # Scala IDE specific (Scala & Java development for Eclipse) |
47 | .cache-main |
48 | .scala_dependencies |
49 | .worksheet |
curl コマンド で グローバル gitignore を 作る
利用する gitignore を 決めたら、グローバル gitignore を 作ります.
Powershell 3.0 から curl
こと Invoke-WebRequest
が 使えます. これでダウンロードすれば OK! (ただし使い方は Linux のとは異なるので注意)
対象が ひとつ の ファイルの場合は、コマンドプロンプトから以下のように実行します. ここでは Windows.gitignore
を ~/.gitignore
へ ダウンロードしました.
1 | c:\Temp> powershell curl -Uri https://raw.githubusercontent.com/github/gitignore/master/Global/Windows.gitignore -OutFile ~/.gitignore |
複数のファイルを結合する場合は、PowerShell を 起動してからコマンドを実行します. ここでは Windows.gitignore
と Eclipse.gitignore
と VisualStudioCode.gitignore
を 結合しました.
1 | c:\Temp> powershell |
2 | Windows PowerShell |
3 | Copyright (C) 2016 Microsoft Corporation. All rights reserved. |
4 | |
5 | PS C:\Temp> |
6 | PS C:\Temp> curl -Uri https://raw.githubusercontent.com/github/gitignore/master/Global/Windows.gitignore -UseBasicParsing | Select-Object -ExpandProperty Content >> ~/.gitignore |
7 | PS C:\Temp> curl -Uri https://raw.githubusercontent.com/github/gitignore/master/Global/Eclipse.gitignore -UseBasicParsing | Select-Object -ExpandProperty Content >> ~/.gitignore |
8 | PS C:\Temp> curl -Uri https://raw.githubusercontent.com/github/gitignore/master/Global/VisualStudioCode.gitignore -UseBasicParsing | Select-Object -ExpandProperty Content >> ~/.gitignore |
gitignore.io で 自動生成
複数ファイルを結合して使う場合に、コマンドを複数発行すればよいのですが ちょっと面倒だなぁという時は gitignore.io というサービスが自動で作ってくれます. なんと 名だたる企業が使っている ようです(が、企業ロゴが1枚画像で企業へのリンク無しなんだよなぁ…)
Web から使う場合は、 https://www.gitignore.io/ へ アクセスします.
画面中央のテキストボックスに gitignore したいテンプレート名前を入れていき、[Create] ボタンをクリックします.
自動生成された gitignore の 内容が出力されるので、コピー&ペーストします.
また、自動生成された gitginore 画面 の URL を 使うことでコマンドラインからも取得できます. キーワードをカンマでつなぐのですが、URL Encode するので %2C
で つなぎます.
1 | c:\Temp> powershell curl -Uri https://www.gitignore.io/api/windows%2Ceclipse%2Cvisualstudiocode -OutFile ~/.gitignore |
ヘルプや、指定できるキーワードのリストは PowerShell から 以下のように実行します.
1 | c:\Temp> powershell |
2 | |
3 | PS C:\Temp> curl -Uri https://www.gitignore.io/api/ -UseBasicParsing | Select-Object -ExpandProperty Content |
4 | gitignore.io help: |
5 | list - lists the operating systems, programming languages and IDE input types |
6 | :types: - creates .gitignore files for types of operating systems, programming languages or IDEs |
7 | |
8 | PS C:\Temp> curl -Uri https://www.gitignore.io/api/list -UseBasicParsing | Select-Object -ExpandProperty Content |
9 | 1c-bitrix,a-frame,actionscript,ada,adobe |
10 | advancedinstaller,agda,alteraquartusii,altium,android ...(省略) |
さらに Command Line Docs - gitignore.io には、公式のコマンドライン実行法について説明があります. スクリプト化しておくと便利なのかもしれませんが、 curl
だけでも大変ありがたいです.
gitignore.io-san, thank you for the wonderful service!!
github/gitignore リポジトリの Global ディレクトリ発見から、便利なサービスにまでたどり着くことができ、開発環境を作る際の幅広がりました. 自分が知っていることをすぐに使えることも必要ですが、他にも良いやり方は無いのかと疑問を持って調べる習慣も大事ですね. Global ディレクトリに気づかなかったのはホント不覚である…
今回は グローバル gitignore の 自動生成でしたが、アプリケーション用の gitignore も 同じことができるので、これからは自動生成でやった方が良いですね.
実際に使うにあたっては、グローバル gitignore は 自動生成後に編集.
アプリケーション用にはプロジェクトなり組織共通の gitignore を 管理するリポジトリを作って、先にそのファイルをダウンロードして、gitignore.io 自動生成を追記させるような感じでしょうか.
言語やフレームワークの gitignore を 自動生成させるとすると、独自追加部分だけが残り、だいたい どのリポジトリも同じものを追加すると思われるので、Git で 管理・共用化して、自動生成だけにするのがよいのかもと思ったりも… 今度やってみよう.