git addは、変更されたファイルをステージングエリアに追加するコマンドです。ステージングエリアに追加された変更が、次のgit commitでコミットされます。
基本的な使い方
特定のファイルを追加
bash
# 1つのファイルを追加
git add file.js
# 複数のファイルを追加
git add file1.js file2.js file3.js
# ワイルドカードを使用
git add *.js
git add src/**/*.js
すべての変更を追加
bash
# カレントディレクトリ以下のすべての変更を追加
git add .
# リポジトリ全体のすべての変更を追加
git add -A
# または
git add --all
ディレクトリごと追加
bash
# ディレクトリ内のすべてのファイルを追加
git add src/
# 複数のディレクトリを追加
git add src/ docs/ tests/
よく使うオプション
-A, --all: すべての変更を追加
bash
git add -A
以下のすべてを含みます:
- 変更されたファイル(Modified)
- 新規ファイル(Untracked)
- 削除されたファイル(Deleted)
リポジトリ全体が対象です。
-u, --update: 追跡済みファイルのみ更新
bash
git add -u
- 変更されたファイル(Modified)
- 削除されたファイル(Deleted)
新規ファイル(Untracked)は含みません。
-p, --patch: 対話的に選択
bash
git add -p
# または
git add --patch
変更を部分的にステージングできます。
plain
Stage this hunk [y,n,q,a,d,s,e,?]?
オプション:
y: この変更をステージングn: この変更をスキップq: 終了a: この変更と以降すべてをステージングd: この変更と以降すべてをスキップs: 変更をさらに小さく分割e: 手動で編集?: ヘルプ表示
-i, --interactive: 対話モード
bash
git add -i
メニュー形式で操作できます。
plain
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
-n, --dry-run: 実際には追加せずに確認
bash
git add -n .
# または
git add --dry-run .
どのファイルが追加されるか確認できます。
plain
add 'src/app.js'
add 'src/utils.js'
-f, --force: .gitignoreを無視して追加
bash
git add -f ignored-file.log
通常は避けるべきですが、例外的に追跡したい場合に使用します。
-v, --verbose: 詳細情報を表示
bash
git add -v .
追加されるファイルの詳細を表示します。
実践的な使い方
変更を確認してから追加
bash
# 変更されたファイルを確認
git status -s
# 差分を確認
git diff
# 問題なければ追加
git add .
一部のファイルだけ追加
bash
# 機能Aに関連するファイルのみ追加
git add src/featureA.js tests/featureA.test.js
# コミット
git commit -m "Add feature A"
# 機能Bは別のコミットに
git add src/featureB.js
git commit -m "Add feature B"
変更の一部だけをステージング
bash
# 対話的に選択
git add -p file.js
# 1つのファイルに複数の変更があるが、一部だけコミットしたい場合に便利
実行例:
bash
# file.jsに2つの変更がある
# 変更1: バグ修正
# 変更2: 新機能追加
git add -p file.js
# → 変更1だけ選択(y)
# → 変更2はスキップ(n)
git commit -m "Fix bug"
# 後で変更2をコミット
git add file.js
git commit -m "Add new feature"
削除されたファイルを反映
bash
# ファイルを削除
rm old-file.js
# 削除を反映
git add -u
# または
git rm old-file.js
新規ファイルだけを追加
bash
# 新規ファイルのみ追加(変更や削除は含まない)
git add $(git ls-files -o --exclude-standard)
git add . vs git add -A vs git add -u
git add .(カレントディレクトリ以下)
bash
git add .
カレントディレクトリとサブディレクトリの:
- 変更されたファイル
- 新規ファイル
- 削除されたファイル
git add -A(リポジトリ全体)
bash
git add -A
リポジトリ全体の:
- 変更されたファイル
- 新規ファイル
- 削除されたファイル
git add -u(追跡済みのみ)
bash
git add -u
リポジトリ全体の:
- 変更されたファイル
- 削除されたファイル
新規ファイルは含まない。
比較表
plain
コマンド | 変更 | 新規 | 削除 | 範囲
---------------|------|------|------|------------------
git add . | ✓ | ✓ | ✓ | カレント以下
git add -A | ✓ | ✓ | ✓ | リポジトリ全体
git add -u | ✓ | ✗ | ✓ | リポジトリ全体
ステージングを取り消す
特定のファイルを取り消す
bash
git restore --staged file.js
# または(Git 2.23以前)
git reset HEAD file.js
すべてのステージングを取り消す
bash
git restore --staged .
# または
git reset HEAD
他のコマンドとの組み合わせ
git statusで確認してからadd
bash
# 変更を確認
git status -s
# 特定のファイルを追加
git add src/app.js
# 追加されたか確認
git status -s
git diffで差分を確認してからadd
bash
# 変更内容を確認
git diff src/app.js
# 問題なければ追加
git add src/app.js
# ステージング済みの差分を確認
git diff --cached
git commitと組み合わせる
bash
# 追加
git add .
# コミット
git commit -m "Update feature"
# または、一度に実行(追跡済みファイルのみ)
git commit -am "Update feature"
よくあるパターン
すべての変更を追加してコミット
bash
git add -A
git commit -m "Complete feature implementation"
一部の変更だけコミット
bash
# バグ修正のみ追加
git add src/bugfix.js
# バグ修正をコミット
git commit -m "Fix critical bug"
# 残りの変更は後でコミット
git add .
git commit -m "Add new features"
変更をレビューしながら追加
bash
# 対話的に確認しながら追加
git add -p
# ステージングされた内容を確認
git diff --cached
# 問題なければコミット
git commit -m "Reviewed changes"
よくある間違いと対処
誤ってすべてを追加してしまった
bash
# ❌ 間違えて実行
git add -A
# ✅ 取り消す
git restore --staged .
# 必要なファイルだけ追加し直す
git add src/app.js
.gitignoreファイルを追加してしまった
bash
# ❌ 間違えて.envを追加
git add .env
# ✅ 取り消す
git restore --staged .env
# .gitignoreに追加
echo ".env" >> .gitignore
git add .gitignore
node_modules/を追加してしまった
bash
# ❌ 間違えて追加
git add node_modules/
# ✅ 取り消す
git restore --staged node_modules/
# .gitignoreに追加
echo "node_modules/" >> .gitignore
git add .gitignore
すでにコミットしてしまった場合:
bash
# 追跡を停止(ファイルは残す)
git rm -r --cached node_modules/
echo "node_modules/" >> .gitignore
git add .gitignore
git commit -m "Remove node_modules from git"
機密情報を追加してしまった
bash
# ステージングから削除
git restore --staged .env
# .gitignoreに追加
echo ".env" >> .gitignore
# 既にコミットしてしまった場合は履歴から削除が必要
# (git filter-branchやBFG Repo-Cleanerを使用)
addを使うタイミング
1. 論理的なまとまりごとにadd
bash
# バグ修正
git add src/bugfix.js tests/bugfix.test.js
git commit -m "Fix login bug"
# 新機能
git add src/feature.js tests/feature.test.js
git commit -m "Add export feature"
2. ファイル編集後、すぐにadd
bash
# ファイルを編集
vim src/app.js
# すぐに追加(後で忘れない)
git add src/app.js
3. 作業の区切りでadd
bash
# 1つの機能が完成したら
git add .
git commit -m "Complete feature X"
4. レビュー後にadd
bash
# 変更内容をレビュー
git diff
# 問題なければ追加
git add .
ベストプラクティス
1. 細かくaddしてコミットを分ける
bash
# ❌ すべてを一度にコミット
git add -A
git commit -m "Update everything"
# ✅ 論理的な単位でコミット
git add src/auth.js
git commit -m "Improve authentication"
git add src/ui.js
git commit -m "Update UI components"
2. git add . よりも具体的に指定
bash
# ❌ すべて追加(意図しないファイルが混入する可能性)
git add .
# ✅ 必要なファイルだけ追加
git add src/
git add tests/
3. addの前に必ずdiffを確認
bash
# 変更内容を確認
git diff
# デバッグコードや不要な変更がないか確認
git add .
4. -pオプションで丁寧に確認
bash
# 大きな変更の場合は対話的に確認
git add -p
5. .gitignoreを先に設定
bash
# プロジェクト開始時に.gitignoreを設定
cat > .gitignore << EOF
node_modules/
.env
*.log
dist/
.DS_Store
EOF
git add .gitignore
git commit -m "Add .gitignore"
なぜステージングエリアが必要か
1. コミットする内容を選択できる
bash
# 10個のファイルを変更したが、3個だけコミットしたい
git add file1.js file2.js file3.js
git commit -m "First feature"
# 残り7個は別のコミット
git add file4.js file5.js file6.js file7.js file8.js file9.js file10.js
git commit -m "Second feature"
2. レビューのタイミングができる
bash
# ステージング(レビューポイント)
git add .
# ステージングされた内容を確認
git diff --cached
# 問題があれば修正
git restore --staged problematic-file.js
3. 論理的なコミットを作れる
作業中は複数の変更を同時に行いますが、コミットは論理的な単位に分けるべきです。
bash
# 作業中:バグ修正と新機能を同時に実装
# ↓
# コミット1:バグ修正のみ
git add bugfix.js
git commit -m "Fix critical bug"
# コミット2:新機能のみ
git add feature.js
git commit -m "Add new feature"
便利なエイリアス
.gitconfigに追加
[alias]
a = add
aa = add -A
au = add -u
ap = add -p
使用例:
bash
git a file.js # git add file.js
git aa # git add -A
git au # git add -u
git ap # git add -p
まとめ
日常的に使うコマンド
bash
# 基本
git add . # カレントディレクトリ以下を追加
git add -A # すべてを追加
# 選択的
git add file.js # 特定ファイル
git add -p # 対話的に選択
# 確認
git add -n . # 追加されるファイルを確認
git status -s # ステージング状態を確認
覚えておくべきポイント
- addはコミットの準備:ステージングエリアに追加するだけ
- git add . vs -A:カレント以下 vs リポジトリ全体
- -uは新規ファイルを含まない:追跡済みファイルのみ
- -pで部分的にステージング:1つのファイルの一部だけコミット可能
- 間違えたら restore --staged:ステージングを取り消せる