技術共有

【Git学習記】第4章 git rebase rebaseの操作と関連例(前編)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

第 4 章 リベース操作と関連事例

【関連トピック】

  • 意思 commit バージョンを別のブランチにリベースする
  • バージョンが競合した場合にリベースを実行する
  • 指定したバージョンで対話型リベースを実行する
  • インタラクティブなリベースを使用してバージョンを集約する
  • インタラクティブなリベースを使用したコミッターの変更
  • 自動集計バージョン

4.0 リベースの概要

リベース (Rebasing ) は、Git の非常に強力な機能です。リベースは操作の一種です。commit 最も初期のものは以下に基づいていましたcommit B の; リベース先、つまり意志 に基づいてなる 手術。

次のデモ ケースでは、リベースが思ったほど簡単ではないことがわかります。

4.1 意志 commit バージョンを別のブランチにリベースする

まず、最も単純なタイプのリベース操作を見てみましょう。関連する準備作業: 新しいファイルの導入、送信、内容の変更、および再送信。このようにして、ローカルの 2 倍の commit バージョン。

まだ一緒に jgit ライブラリの例:

# Clone jgit repo into chapter4
$ git clone https://git.eclipse.org/r/jgit/jgit chapter4
$ cd chapter4
# Checkout a new branch
$ git checkout -b rebaseExample --track origin/stable-3.1
# Create the 1st commit
$ echo "My Fishtank
    
Gravel, water, plants
Fish, pump, skeleton" > fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Create the 2nd commit
$ echo "mosquitos" >> fishtank.txt
$ git add fishtank.txt
$ git commit -m "Feeding my fish"
# Rabase to stable-3.2
$ git rebase origin/stable-3.2
Successfully rebased and updated refs/heads/rebaseExample.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

リベースする前に:

ここに画像の説明を挿入します

リベース後:

ここに画像の説明を挿入します

git rebase 実行プロセス:

  1. 現れる HEAD リベースが指すターゲットブランチ間の共通バージョン (merge-base);
  2. に基づく merge-base、ターゲット ブランチ上で欠落しているバージョンをすべて検索します。
  3. 不足しているバージョンをターゲット ブランチに 1 つずつ適用してみてください。

4.2 バージョン競合が発生した場合のリベースの実行

もし commit バージョンまたはbranch ブランチを別のブランチにリベースするHEAD 、バージョンの競合が発生する可能性があります。この時点で競合を解決し、コマンドを実行する必要があります。git rebase --continue、リベースを完了できます。

このセクションの例では、競合が存在する場合にリベースを実行する方法を示します。セクション 4.1 で示した最終結果に続き、今回は rebaseExample ブランチは以下にリベースされましたstable-3.2 支店。この例では、stable-3.1 からの新しいブランチを再チェックアウトし、 と を追加します。rebaseExample ブランチ名は同じだが内容に互換性がないテキスト ファイルfishtank.txt

# Checkout rebaseExample2
$ git checkout -b rebaseExample2 --track origin/stable-3.1
# Add a new commit
$ echo "My Fishtank
Pirateship, Oister shell
Coconut shell
">fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Rebase conflicting branches
$ git rebase rebaseExample
Auto-merging fishtank.txt
CONFLICT (add/add): Merge conflict in fishtank.txt
error: could not apply 24f9bf1ef... My brand new fishtank2
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 24f9bf1ef... My brand new fishtank2
$ subl fishtank.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

紛争の状況は次のとおりです。

ここに画像の説明を挿入します

次の内容に変更し、保存して閉じます。

ここに画像の説明を挿入します

マージされたファイルを追加し、リベースを続行します。

$ git add fishtank.txt
$ git rebase --continue
hint: Waiting for your editor to close the file...
  • 1
  • 2
  • 3

開かれるエディターを次の図に示します。

ここに画像の説明を挿入します

確認して保存して閉じると、リベースが成功したことを示すプロンプトが表示されます。

$ git rebase --continue
[detached HEAD 9911772b3] My brand new fishtank2
 1 file changed, 2 insertions(+), 3 deletions(-)
Successfully rebased and updated refs/heads/rebaseExample2.
# Check new status via gitk
$ gitk
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

詳細は以下のとおりです。

ここに画像の説明を挿入します

このリベースでは、古いブランチには存在するが新しいブランチには存在しないバージョンのみがリベースされる (つまり、新しいバージョンが追加される) ことがわかります。 commit)。

最初のリベース中断の git プロンプト情報には、次の 2 つの選択肢も表示されます。

  1. git rebase --abort: 文字通りの意味として、リベースを中断します。

  2. git rebase --skip: 競合をスキップして直接リベースすると、次のようになります。 rebaseExample2 新しいものを捨てるcommitに直接マージされます rebaseExample、リベース後に指された親、および rebaseExample 一貫性のある:

    ここに画像の説明を挿入します


4.3 指定したバージョンで対話型リベースを実行する

この例では、 rebaseExample ブランチに基づいて使用方法をデモンストレーションする--interactive マーク、この 2 つを 4.1 でリベースしてくださいcommit リモート追跡ブランチへのリベースstable-3.1 優れた:

$ git checkout rebaseExample
Switched to branch 'rebaseExample'
Your branch is ahead of 'origin/stable-3.1' by 109 commits.
  (use "git push" to publish your local commits)
$ git rebase origin/stable-3.1
  • 1
  • 2
  • 3
  • 4
  • 5

このとき、新たにポップアップエディターにコミットリストが表示されます。 rebaseExample そしてstable-3.1 間、すべてを次のようにリベースできますstable-3.1commit 記録。例では 2 つの新しいものをそのままにしておきますcommit、残りをすべて削除します (つまり、89 行目以前の内容)。

ここに画像の説明を挿入します

保存後、エディタを終了すると、次の出力が表示されます。

$ git rebase --interactive origin/stable-3.1
Successfully rebased and updated refs/heads/rebaseExample.
# Check in gitk view:
  • 1
  • 2
  • 3

結果は次のとおりです。

ここに画像の説明を挿入します

拡張子の例

この例では、次のコマンドを使用して目的の効果をすばやく実現することもできます。

$ git rebase --onto origin/stable-3.1 origin/stable-3.2 rebaseExample
Successfully rebased and updated refs/heads/rebaseExample.
  • 1
  • 2

リベース前後の図は次のとおりです。

ここに画像の説明を挿入します