#.gitignore を使ったファイルの無視
時々、一時ファイルや機密情報など、Gitが追跡したくないファイルがあります。Gitは、.gitignore
と呼ばれる特別なファイルを使って、これらのファイルを無視することができます。
.gitignore
ファイルを作成しましょう。
echo "*.log" > .gitignore
このコマンドは、.log
拡張子を持つ任意のファイルをGitに無視させるように指示する .gitignore
ファイルを作成します。
次に、.gitignore
をテストするためにログファイルを作成しましょう。
echo "This is a log file" > debug.log
リポジトリの状態を確認します。
git status
出力に debug.log
がリストされていないことがわかりますが、.gitignore
はリストされています。
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
.gitignore
ファイルは非常に強力です。特定のファイルや、ディレクトリ全体を無視することができますし、特定の基準に一致するファイルを無視するためにパターンマッチングを使うこともできます。これは、ビルド生成物、キャッシュファイル、または環境固有の設定ファイルなど、Gitリポジトリの一部に含めるべきでないものがある現実世界のプロジェクトで非常に便利です。
変更を追加してコミットしましょう。
git add.gitignore
git commit -m "Initial commit with hello.py and.gitignore"