0
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の書き方と注意点を最小限で整理

Last updated at Posted at 2025-10-23

はじめに

例外の書き方でハマったので備忘録として書いてみました!
なるべく最低限の情報をまとめています

.gitignore とは

Gitリポジトリ内で追跡しないファイルを指定するための設定ファイル
.gitignore に記述されたパターンにマッチする未追跡ファイルが除外対象

書き方

基本ルール

記号 意味
# コメント
! 例外
/ ディレクトリ区切り
* 任意の文字列(スラッシュ以外)
? 任意の1文字
** 任意階層にマッチ
[a-z] 文字の範囲指定

よくある例

.gitignore
# ビルド生成物の除外
dist/
build/
src/*.js
src/**/*.js

# 依存ライブラリなど
node_modules/

# 特定ファイルのみ追跡
.env*
!.env.example

# 任意の階層
**/logs/*.log

注意点

  • 既に追跡されているファイルは対象外(git rm --cached などで対応)
  • パターンの記述が相対パスベース
  • 親ディレクトリが ignore されてしまうと、その配下の例外指定(!) が機能しない
.gitignore
# NG例: 親ディレクトリごと除外してしまっている
logs/
!logs/important.log  # 効かない

# OK例
logs/*
!logs/important.log

その他の除外設定

.git/info/excludecore.excludesFile など追跡しないようにする方法は他にもある

使い分けの例

  • リポジトリのチーム共通で除外したいもの → .gitignore
  • リポジトリ固有でチーム共有不要なもの → .git/info/exclude
  • リポジトリ関係なく個人環境固有で除外したいもの → core.excludesFile

どの設定で除外されたかは git check-ignore で確認できる

参考

おわりに

以上です!読んでいただきありがとうございます!

0
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
0
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?