0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】派生元ブランチを変更したいときの git rebase --onto

Posted at

Git を使っていると、ブランチを間違ったところから切ってしまうことがあります。

「本当は feature-A から feature-B を切るつもりだったのに、間違えて main から feature-B を切って作業してしまった…」

そんなときの解決方法が git rebase --onto です。
このコマンドを使えば、すでにあるブランチの派生元をあとから変更することができます。

想定される状況

以下のようなブランチ構成になっているとします。

A---B---C               (main)
         |\
         | D---E        (feature-A)
         |
          F---G         (feature-B)

feature-A は main のコミット C から正しく切られている。

feature-B は、本来 feature-A から切るべきだったが、誤って main から切ってしまった。

解決策: git rebase --onto の使い方

このようなとき、次のコマンドで feature-B の派生元を feature-A に切り替えることができます。

git rebase --onto feature-A main feature-B

このコマンドの意味は以下のとおりです。

  • feature-A: 新しい派生元

  • main: 今の派生元

  • feature-B: 派生元を変更したいブランチ

リベースが成功すると、ブランチ構成は次のようになります。

A---B---C                   (main)
         \
          D---E             (feature-A)
                 \
                  F---G     (feature-B)

まとめ

git rebase --onto の基本構文はこちらです。

git rebase --onto <新しい派生元> <今の派生元> <派生元を変更したいブランチ>

このコマンドを使えば、間違って作ってしまったブランチの派生元を、あとから正しいブランチに変更することができます。
ブランチの切り間違いに気づいても、最初からやり直す必要はありません。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?