Rebase: 履歴の整理
コミット履歴を直線的に整理し、読みやすくする。
用語集
Rebase
コミットを別のベースに再適用。
Interactive
コミットを編集・整理しながらrebase。
Squash
複数コミットを1つに統合。
Force Push
リモート履歴を上書き(危険)。
基本Rebase(mainに追従)
feature ブランチを最新の main に追従させる。
Merge
# Merge creates a merge commit# History looks like:## main ──●──●──────●──M──# \ /# feature ●──●──●Rebase
# Rebase creates linear history# History looks like:## main ──●──●──●──●──●──●# ↑# feature commits re-applied# Sync feature branch with latest maingit switch feature/user-profilegit fetch origingit rebase origin/main
# If conflicts occur:# 1. Fix conflicts in your editor# 2. Stage resolved filesgit add resolved_file.ts# 3. Continue rebasegit rebase --continue
# Or abort if things go wronggit rebase --abort実行結果
Successfully rebased and updated refs/heads/feature/user-profile.
Interactive Rebase(履歴編集)
コミットの編集、統合、削除ができる強力なツール。
# Edit last 3 commitsgit rebase -i HEAD~3
# In the editor, you'll see:pick abc1234 feat: Add user formpick def5678 fix: Typo in validationpick ghi9012 feat: Add email field
# Change 'pick' to:# p, pick = use commit as-is# r, reword = use commit, but edit message# e, edit = use commit, but stop for amending# s, squash = meld into previous commit (keep message)# f, fixup = meld into previous (discard message)# d, drop = remove commit entirelySquash の例
# Before: Messy commitspick abc1234 feat: Add user formpick def5678 wippick ghi9012 fix typopick jkl3456 oops
# After: Clean squashpick abc1234 feat: Add user formsquash def5678 wipsquash ghi9012 fix typosquash jkl3456 oops
# Result: One clean commit with combined message Tip: PR前に git rebase -i で「wip」「fix typo」などの雑なコミットを整理すると、レビューが楽になる。
Force Push(履歴の上書き)
警告: rebase後は通常のpushが拒否されます。force pushが必要ですが、他の人の作業を壊す可能性があります。
# After rebase, history has changed# Regular push will be rejected
# Use --force-with-lease (safer than --force)git push --force-with-lease origin feature/user-profile
# --force-with-lease checks if anyone else pushed# before overwriting (prevents data loss) ゴールデンルール: 自分だけが使っているブランチでのみ force push する。共有ブランチ (main, develop) では絶対にしない。
合格ライン
git rebase で main に追従できる
git rebase -i でコミットを整理できる
squash と fixup の違いを説明できる
force push のリスクを理解している
参考リンク
演習課題
課題1: 基本的なリベース
feature ブランチを main にリベースしてみてください。履歴が直線的になることを確認してください。
課題2: Interactive Rebase
git rebase -i HEAD~3 を使って、コミットを squash してみてください。