30日でマスター Git バージョン管理

Day 23: コミット済み履歴の修正 Part 5(rebase 2)

前回の記事では、Rebase は既存の 2 つのブランチに対して「ベースバージョンを再指定する」ために使われることを説明しました。Rebase を実行すると、元のブランチの開始点も変更され(分岐点が移動し)、バージョングラフに変化が生じます。しかし、Rebase でできることはそれだけではありません。特定のブランチライン上の任意のバージョンのバージョン情報を修正するためにも使えます。

本記事では git rebase の探究を続け、ブランチ上のコミット情報の修正を含め、Rebase を効果的に使うための高度なテクニックと実践的なワークフローを深く掘り下げます。

高度なインタラクティブ Rebase のテクニック

コミットを分割する

1 つのコミットを複数に分割するには、以下のようにします。

git rebase -i HEAD~3

# Mark the commit to split with "edit"
# When Git stops at that commit:
git reset HEAD^
git add file1
git commit -m "First part"
git add file2
git commit -m "Second part"
git rebase --continue

結合と並べ替え

1 回の操作で、コミットの並べ替えと結合を同時に行うことができます。

git rebase -i HEAD~5

# In the editor:
pick abc123 Feature A
squash def456 Fix for A
pick jkl012 Feature C  # Moved before B
pick ghi789 Feature B  # Moved after C
fixup mno345 Typo fix for B

exec を使った Rebase

Rebase 中に各コミットの後にコマンドを実行します。

git rebase -i --exec "npm test" HEAD~5

これにより、各コミットを適用した後に npm test が実行され、どのコミットがテストを壊したかを見つけるのに役立ちます。

特定のコミットへの Rebase

ブランチ名の代わりに、特定のコミットへ Rebase することもできます。

git rebase --onto <new-base-commit> <old-base-commit> <branch>

ユースケースの例:履歴の途中からコミットを削除する

# Remove commits between B and D
#     A---B---C---D---E feature
# Becomes:
#     A---B---D'---E' feature

git rebase --onto B D feature

ストラテジオプションを使った Rebase

Rebase 中にマージストラテジを使います。

# Always prefer our version in conflicts
git rebase -X ours main

# Always prefer their version
git rebase -X theirs main

マージコミットを保持する

デフォルトでは、Rebase はマージコミットを平坦化します。保持するには、以下を使います。

git rebase -p main
# or
git rebase --preserve-merges main

注意:-p は非推奨です。代わりに --rebase-merges を使ってください。

git rebase --rebase-merges main

Fork-Point を使った Rebase

フォークポイントを自動的に見つけます。

git rebase --fork-point main

これは、リモートブランチが強制プッシュされた場合に便利です。

ワークフロー:機能ブランチを最新に保つ

機能ブランチを main と同期させ続けるための一般的なワークフローは以下のとおりです。

# On feature branch
git fetch origin
git rebase origin/main

# Or in one command
git pull --rebase origin main

ワークフロー:プルリクエストの準備

プルリクエストを作成する前にコミットを整理します。

# 1. Interactive rebase to clean history
git rebase -i main

# 2. Squash fixup commits, reword messages
# 3. Force push to update your branch
git push -f origin feature-branch

ワークフロー:コードレビュー後に更新する

コードレビューのフィードバックを受けた後は、以下のようにします。

# Make changes
git add .
git commit --fixup <original-commit>

# Rebase with autosquash
git rebase -i --autosquash main

# Force push the updated branch
git push -f origin feature-branch

強制プッシュへの対処

作業中のブランチを誰かが強制プッシュした場合は、以下のようにします。

# Fetch the updated branch
git fetch origin

# Rebase your work onto the new history
git rebase --onto origin/feature-branch old-commit feature-branch

または、シンプルに以下のようにします。

git pull --rebase origin feature-branch

クリーンアップにおける Rebase と Reset の比較

プッシュ前にローカルコミットを整理する場合:

Rebase(推奨):

  • 時系列の順序を保持します
  • インタラクティブに制御できます
  • コミットを cherry-pick できます

Reset(シンプルだが柔軟性に欠けます):

  • コミットを完全に削除します
  • クリーンな状態からやり直します
  • 個々のコミットに対する制御が限られます

よくある Rebase のパターン

パターン 1:すべてのコミットをスカッシュする

git rebase -i --root
# Mark all but first commit as "squash"

パターン 2:すべてのコミットメッセージを書き換える

git rebase -i HEAD~10
# Mark all commits as "reword"

パターン 3:特定のコミットを削除する

git rebase -i HEAD~10
# Mark unwanted commits as "drop"

Rebase の安全チェックリスト

Rebase する前に確認します。

  • このコミットは共有ブランチにプッシュされていますか?
  • 他の人がこのコミットをベースに作業していますか?
  • バックアップ(ブランチまたはタグ)はありますか?
  • チームとコミュニケーションを取りましたか?
  • 強制プッシュの準備はできていますか?

Rebase の大失敗から復旧する

問題が発生した場合は、以下のようにします。

# Step 1: Stay calm
# Step 2: Find the old state
git reflog

# Step 3: Reset to before the rebase
git reset --hard HEAD@{5}

# Step 4: Try again more carefully

Rebase と継続的インテグレーション

CI/CD と Rebase を併用する場合は、以下のようにします。

  1. ローカルで Rebase する:プッシュする前に行います
  2. テストを実行する:Rebase の後に行います
  3. 強制プッシュする:自分のブランチだけにします
  4. CI を待つ:マージする前に確認します
  5. マージ後は決して Rebase しない

ベストプラクティスのまとめ

  1. ローカルのみ:Rebase するのはプッシュしていないコミットだけにします
  2. 小さな Rebase:大きな競合を避けるため、頻繁に Rebase します
  3. 十分にテストする:Rebase の後にテストを実行します
  4. コミュニケーションを取る:強制プッシュする前にチームに警告します
  5. バックアップする:大規模な Rebase の前にバックアップブランチを作成します
  6. -f に注意する:強制プッシュする前によく確認します

本日のまとめ

高度な Rebase のテクニックは、クリーンで分かりやすいコミット履歴を維持するための強力なツールを提供します。これらの機能はワークフローを大幅に改善できますが、練習と慎重な使用が必要です。重要なのは、Rebase を使うべき場面と、Merge のようなより安全な代替手段にとどめるべき場面を知ることです。

本日学んだ Git コマンドとパラメータを整理しておきます。

  • git rebase –exec
  • git rebase –onto
  • git rebase -X
  • git rebase –preserve-merges
  • git rebase –rebase-merges
  • git rebase –fork-point
  • git rebase –root
  • git pull –rebase
  • git push -f