タイトル: ブランチの作成, 一覧表示, 切り替え
SEOタイトル: Git ブランチ操作完全ガイド (作成/切替/削除)
| この記事の要点 |
|
ブランチとは
Git のブランチは、コミット履歴の軽量なポインタです。Subversion 等と違い、ブランチ作成はファイルコピーを伴わず、数ミリ秒で完了します。
標準的な開発フローでは、main から派生してフィーチャーブランチで開発 → main に取り込むパターンを繰り返します。
ブランチの作成
# 1. 現在のコミットから新規ブランチ作成(切替はしない)
git branch feature/login
# 2. 作成 + 即切替(Git 2.23+ 推奨)
git switch -c feature/login
# 3. 旧コマンド(広く流通)
git checkout -b feature/login
# 4. 別のブランチを基点に作成
git switch -c feature/login develop
git branch feature/login develop
# 5. 特定コミットから作成
git switch -c hotfix/critical abc1234
# 6. リモートのブランチを追跡して作成
git switch -c feature/login origin/feature/login
# 短縮: 同名ブランチがリモートに有る場合
git switch feature/login # 自動で origin/feature/login を追跡
ブランチ一覧
# ローカルブランチ一覧(* が現在のブランチ)
git branch
# リモート含む
git branch -a
# リモートのみ
git branch -r
# 詳細(最新コミット表示)
git branch -v
git branch -vv # upstream トラッキング情報も
# マージ済 / 未マージで絞り込み
git branch --merged main # main にマージ済みのブランチ
git branch --no-merged main # まだ取り込まれていない
# 名前で絞り込み
git branch --list "feature/*"
git branch -a --list "*hotfix*"
# 最終更新時刻順
git for-each-ref --sort=-committerdate refs/heads/ \
--format='%(refname:short) %(committerdate:short) %(authorname)'
ブランチの切替
# 切替(Git 2.23+ 推奨)
git switch main
git switch feature/login
# 旧コマンド
git checkout main
# 直前のブランチに戻る
git switch -
git checkout -
# 未コミット変更があると切替できないことがある
# → stash で退避するか、commit してから切替
git stash
git switch other-branch
git switch -
git stash pop
switch / restore vs checkout
Git 2.23(2019 年)で checkout が switch と restore に分割されました:
| 用途 | 旧 | 新(推奨) |
|---|---|---|
| ブランチ切替 | git checkout branch | git switch branch |
| ブランチ作成+切替 | git checkout -b new | git switch -c new |
| ファイル復元(HEAD から) | git checkout -- file | git restore file |
| ファイル復元(特定コミットから) | git checkout abc1234 -- file | git restore --source=abc1234 file |
| ステージ解除 | git reset HEAD file | git restore --staged file |
| detached HEAD でコミット参照 | git checkout abc1234 | git switch --detach abc1234 |
checkout は今も動作しますが、新規学習者は switch / restore を覚えるべき。役割が明確で誤操作しにくい。
ブランチの削除
# マージ済ブランチを削除(安全)
git branch -d feature/login
# 未マージブランチを強制削除
git branch -D feature/abandoned
# リモートブランチを削除
git push origin --delete feature/login
# 旧構文(コロン前置)
git push origin :feature/login
# リモートで削除されたブランチをローカル側でも掃除
git fetch --prune
git remote prune origin
# 一括削除: main 以外の全マージ済ローカル
git branch --merged main | grep -v "^\*\|main\|develop" | xargs -n 1 git branch -d
ブランチ名の変更
# 現在のブランチをリネーム
git branch -m new-name
# 他のブランチをリネーム
git branch -m old-name new-name
# リモートに反映
git push origin -u new-name
git push origin --delete old-name
# master → main 変更(昔のリポジトリ整理)
git branch -m master main
git push -u origin main
git push origin --delete master
# GitHub: Settings → Default branch を main に変更
命名規則(Git Flow / GitHub Flow)
大きなプロジェクトではブランチ名にプレフィックスを付けて役割を明示します:
| プレフィックス | 用途 | 例 |
|---|---|---|
feature/ | 新機能開発 | feature/user-login |
fix/ | バグ修正 | fix/password-reset-error |
hotfix/ | 本番緊急修正 | hotfix/security-patch |
release/ | リリース準備 | release/v2.5.0 |
refactor/ | リファクタ | refactor/auth-service |
chore/ | 雑務(CI / 依存更新) | chore/update-deps |
docs/ | ドキュメント | docs/api-spec |
test/ | テスト追加 | test/login-e2e |
命名のコツ: kebab-case / 英小文字 / 日本語禁止 / 50 文字以内 / Issue 番号併記(例 feature/123-login)。
リモートブランチ追跡
# upstream 設定
git push -u origin feature/login
# → 次回から git push / git pull だけでよくなる
# 後から設定
git branch --set-upstream-to=origin/feature/login feature/login
git branch -u origin/feature/login
# 自動 upstream 設定
git config --global push.autoSetupRemote true
# → push -u を省略可能に
# 追跡状態確認
git branch -vv
# main abc1234 [origin/main] Latest
# * feature/login def5678 [origin/feature/login: ahead 2] WIP
典型的なフィーチャーブランチ運用
# 1. main を最新化
git switch main
git pull origin main
# 2. フィーチャーブランチを作成
git switch -c feature/123-user-login
# 3. 開発 + コミット
echo "..." > src/Login.php
git add src/Login.php
git commit -m "feat: add user login form"
# 4. リモートに push(upstream 設定)
git push -u origin feature/123-user-login
# 5. GitHub / GitLab で PR / MR 作成、レビューを受ける
# 6. main にマージされたらローカルでも更新
git switch main
git pull origin main
git branch -d feature/123-user-login # ローカル削除
git fetch --prune # リモート参照も削除
detached HEAD(外れた HEAD)
ブランチではなく特定コミットを直接 checkout すると detached HEAD 状態になります:
git switch --detach abc1234
# HEAD is now at abc1234 ...
# You are in 'detached HEAD' state. ...
# この状態で commit してもどのブランチにも属さない
# → 残したい場合はブランチ作成
git switch -c experimental
git commit -m "experiment"
# 元に戻る
git switch main
FAQ
Q: ローカルから消したブランチを復活させたい
A: git reflog で過去の HEAD 位置を確認 → git switch -c branch-name HEAD@{5} 等で復元。reflog は 90 日保持されます。
Q: リモートブランチが消えたのに git branch -r に残る
A: git fetch --prune または git remote prune origin で削除。git config --global fetch.prune true で自動化可能。
Q: master と main どっち?
A: main が現在の業界標準。GitHub も 2020 年 10 月以降 main がデフォルト。既存リポは git branch -m master main で変更。
Q: feature ブランチがレビュー中に main が進んだ
A: git fetch origin && git rebase origin/main または git merge origin/main。rebase は履歴直線化、merge はマージコミット追加、チーム方針で選ぶ。
関連
- git switch / restore — checkout の後継
- git merge — ブランチを取り込む
- git rebase — ブランチを別の base に積み直す
- git worktree — 同一リポジトリの複数ブランチを別ディレクトリで同時作業