特定のコミットをgitの別のブランチに基づいて移動するにはどうすればよいですか? 質問する

特定のコミットをgitの別のブランチに基づいて移動するにはどうすればよいですか? 質問する

状況:

  • マスターはXです
  • quickfix1はX + 2コミットです

そのような:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

その後、quickfix2 の作業を開始しましたが、誤って、マスターではなく quickfix1 をコピーするソース ブランチとして選択しました。現在、quickfix2 は X + 2 コミット + 2 関連コミットの状態です。

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

ここで、quickfix2 を含むブランチを作成しますが、quickfix1 に属する 2 つのコミットは含めません。

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

quickfix2 の特定のリビジョンからパッチを作成しようとしましたが、パッチはコミット履歴を保持しません。コミット履歴を保存しながら、quickfix1 に変更のないブランチを残す方法はありますか?

ベストアンサー1

これは典型的な例ですrebase --onto:

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

だからあなたは

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

に:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

これは、クリーンな作業ツリーで行うのが最適です。
見るgit config --global rebase.autostash true、 特にGit 2.10以降

おすすめ記事