{"meta":{"title":"コマンドラインで Git リベースを使う","intro":"コマンドラインで git rebase を使うための短いチュートリアルです。","product":"概要","breadcrumbs":[{"href":"/ja/get-started","title":"概要"},{"href":"/ja/get-started/using-git","title":"Git の使用"},{"href":"/ja/get-started/using-git/using-git-rebase-on-the-command-line","title":"Git リベース"}],"documentType":"article"},"body":"# コマンドラインで Git リベースを使う\n\nコマンドラインで git rebase を使うための短いチュートリアルです。\n\n## Git リベースの使用\n\nこの例では、`git rebase` を除く、利用可能なすべての `exec` コマンドについて説明します。\n\nターミナルで `git rebase --interactive HEAD~7` と入力してリベースを開始します。 お気に入りのテキストエディタに以下の行が表示されます:\n\n```text\npick 1fc6c95 Patch A\npick 6b2481b Patch B\npick dd1475d something I want to split\npick c619268 A fix for Patch B\npick fa39187 something to add to patch A\npick 4ca2acc i cant' typ goods\npick 7b36971 something to move before patch B\n```\n\nこの例では、以下を行います:\n\n* ```\n          `fa39187` を使って、5 番目のコミット (`\"Patch A\"`) を `1fc6c95` コミット (`squash`) にスカッシュします。\n  ```\n* 最後のコミット (`7b36971`) を `\"Patch B\"` コミット (`6b2481b`) の前に移動し、`pick` のままにしておきます。\n* ```\n          `\"A fix for Patch B\"` コミット (`c619268`) を `\"Patch B\"` コミット (`6b2481b`) にマージし、`fixup` を使ってコミット メッセージを無視します。\n  ```\n* ```\n          `dd1475d` を使って、3 番目のコミット (`edit`) を 2 つの小さなコミットに分割します。\n  ```\n* ```\n          `4ca2acc` を使って、スペルミスのあるコミット (`reword`) のコミット メッセージを修正します。\n  ```\n\nお疲れさまでした。 やることが多くて大変なように見えますが、落ち着いて、一度に 1 ステップずつ進めましょう。すぐに慣れて素早くできるようになります。\n\nはじめに、ファイル内のコマンドを次のように変更します:\n\n```text\npick 1fc6c95 Patch A\nsquash fa39187 something to add to patch A\npick 7b36971 something to move before patch B\npick 6b2481b Patch B\nfixup c619268 A fix for Patch B\nedit dd1475d something I want to split\nreword 4ca2acc i cant' typ goods\n```\n\n各行のコマンドを、`pick` から目的のコマンドに変更しました。\n\nそれでは、エディタを保存して閉じます。 これにより、対話的なリベースを開始します。\n\nGit は、最初のリベース コマンド `pick 1fc6c95` では何もする必要がないので、スキップします。 次のコマンド `squash fa39187` に進みます。 この操作には入力が必要なので、Git はもう一度テキストエディタを開きます。 開かれるファイルは次のようになります:\n\n```text\n# This is a combination of two commits.\n# The first commit's message is:\n\nPatch A\n\n# This is the 2nd commit message:\n\nsomething to add to patch A\n\n# Please enter the commit message for your changes. Lines starting\n# with '#' will be ignored, and an empty message aborts the commit.\n# Not currently on any branch.\n# Changes to be committed:\n#   (use \"git reset HEAD <file>...\" to unstage)\n#\n# modified:   a\n#\n```\n\nこのファイルは Git の言い方で、「ほら、これが今この `squash` でやろうとしていることです」という意味です。 最初のコミットのメッセージ (`\"Patch A\"`) と 2 番目のコミットのメッセージ (`\"something to add to patch A\"`) がリストになっています。 これらのコミットメッセージに満足したら、ファイルを保存してエディタを閉じることができます。 それ以外の場合は、テキストを変更するだけでコミットメッセージを変更することができます。\n\nエディタを閉じると、リベースは続行されます:\n\n```text\npick 1fc6c95 Patch A\nsquash fa39187 something to add to patch A\npick 7b36971 something to move before patch B\npick 6b2481b Patch B\nfixup c619268 A fix for Patch B\nedit dd1475d something I want to split\nreword 4ca2acc i cant' typ goods\n```\n\nGit は 2 つの `pick` コマンドを処理します (`pick 7b36971` と `pick 6b2481b`)。 それはまた、対話が不要なため、`fixup` コマンド `fixup c619268` を処理します。\n`fixup` では `c619268` からの変更を、その前のコミットである `6b2481b` にマージします。 両方の変更は同じコミット メッセージ `\"Patch B\"` を持つことになります。\n\nGit は `edit dd1475d` の操作に到達すると停止し、ターミナルに次のメッセージを表示します。\n\n```shell\nYou can amend the commit now, with\n\n        git commit --amend\n\nOnce you are satisfied with your changes, run\n\n        git rebase --continue\n```\n\nこの時点で、プロジェクト内の任意のファイルを編集して追加の変更を加えることができます。 変更するたびに、新しいコミットを実行する必要があります。そのためには、`git commit --amend` コマンドを入力します。 すべての変更を終えたら、`git rebase --continue` を実行できます。\n\nGit は次に `reword 4ca2acc` コマンドに到達します。 テキストエディタがもう一度開き、次の情報が表示されます:\n\n```text\ni cant' typ goods\n\n# Please enter the commit message for your changes. Lines starting\n# with '#' will be ignored, and an empty message aborts the commit.\n# Not currently on any branch.\n# Changes to be committed:\n#   (use \"git reset HEAD^1 <file>...\" to unstage)\n#\n# modified:   a\n#\n```\n\n以前と同様に、Git は編集するためのコミットメッセージを表示しています。 テキスト (`\"i cant' typ goods\"`) を変更し、ファイルを保存してエディターを閉じます。 Git はリベースを終了して、端末に戻ります。\n\n## リベースされたコードをGitHubにプッシュする\n\nGit 履歴を変更したので、通常の `git push origin` は**機能しません**。 最新の変更を「強制プッシュ」することでコマンドを修正する必要があります。\n\n```shell\n# Don't override changes\n$ git push origin main --force-with-lease\n\n# Override changes\n$ git push origin main --force\n```\n\n> \\[!WARNING]\n> フォースプッシュは、ブランチに対するコミットの履歴上の順序を変更するので、深刻な影響を及ぼします。 特にあなたのリポジトリが複数の人々によってアクセスされている場合は、注意して使用してください。\n\n## 参考資料\n\n* ```\n          [AUTOTITLE](/get-started/using-git/resolving-merge-conflicts-after-a-git-rebase)\n  ```"}