1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

よく分かる .gitignore と .gitkeep

Last updated at Posted at 2025-09-02

.gitignore.gitkeep の目的

  • Git のよくあるお悩み
    • 成果物やログをコミットしたくない
    • 空のディレクトリを Git に残したい
    • 一時ファイルや設定ファイルで差分が増えて困る

これらを解決するのが .gitignore.gitkeep だ!

.gitignore について

  • Git が管理しないファイルやディレクトリを指定するファイル
  • チーム開発で不要な差分を出さない、個人開発でも快適に作業するために必須
  • プロジェクトのルートに .gitignore を置くのが基本
    • gitignore はファイル名 .gitignore である

基本ルール

コメント

# これはコメント

ファイル単体を無視

unnecessary.txt

ディレクトリを無視

logs/

特定の拡張子を無視

*.log
*.tmp

サブディレクトリも含めて無視

**/experiments/

リポジトリ直下だけ対象

/config.json    # ルート直下のみ

特定ファイルを例外的に無視しない

*.log   # 拡張子 log は全て無視する
!important.log   # ただしこのファイルは無視しない

ワイルドカード

  • *: 任意の文字列
  • ?: 任意の1文字
  • [0-9]: 文字クラス
  • **: ディレクトリ階層をまたぐ

具体例

.gitignore
# OS / エディタ
.DS_Store
Thumbs.db
*.swp
.vscode/

# Python / AI関連
__pycache__/
*.py[cod]    # *.pyc *.pyo *.pyd をまとめて無視する
*.egg-info/
*.pytest_cache/
*.ipynb_checkpoints/

# 環境設定ファイル
.env
config.yaml

# データ / 実験結果
/data/    # データセット
/results/    # 実験結果やモデル出力 (中身も無視)
/checkpoints/    # 学習モデルのチェックポイント
*.h5
*.pt
*.ckpt

# ビルド成果物
build/
dist/
*.o
*.class

# 依存関係
venv/
env/
node_modules/
vendor/

.gitignore の確認方法

無視されているファイルを確認出来る

git status --ignored

既にコミットされたファイルは無視されないので注意

キャッシュから削除して .gitignore に従わせる

git rm --cached ファイル名

.gitkeep について

  • Git は 空のディレクトリは管理出来ない
  • プロジェクト構造を保ちたいときは、空ディレクトリに .gitkeep を置く

構造の例

logs/
└─ .gitkeep
  • これで logs/ ディレクトリ自体も Git 管理される
  • .gitkeep は慣習名
  • 中身は空で OK
  • .gitignorelogs/ を無視している場合は .gitkeep を例外として残すようにする
.gitignore
logs/*
!logs/.gitkeep
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?