LoginSignup
8
11

More than 5 years have passed since last update.

Git で混ぜこぜでコミットしたファイルを切り出してブランチを作っていく

Posted at

まぜこぜでコミットした後に、後からやっぱり分割したくなったときにどうするか、色々なやり方があると思う。
ケースによっては git reset したほうが早かったり、 git cherry-pick 使うかもしれない。

他のケースを試してみる前に、まずは自分の知ってる方式ではどうやってるのか、記事にしてみた。

もっと良いやり方があるかもしれない。

順番

この記事内では

4 つのファイルを 1 つに混ぜこぜでコミット → 4 つの各ブランチに分割 → 新しいブランチにマージ → 古いブランチをリベース

の順で行った。

リモートに push したブランチを rebase するのは推奨されていないので、その場合は最後にリベースしてはいけない。
3.6 Git のブランチ機能 - リベース - ほんとうは怖いリベース - 公開リポジトリにプッシュしたコミットをリベースしてはいけない

ブランチの命名について

自分は、普段は Git Flow か GitHub Flow のどちらかを使っている。
説明の主旨とは関係ないので、この記事はどちらのフローでもない。

練習のため初期化

$ mkdir testing
$ cd testing
$ git init
$ git commit --allow-empty -am 'Initial'

前提: ファイル a b c d を 1 度に commit してしまった。

$ git checkout -b base
$ git checkout -b ABCD
$ touch a b c d
$ git add a b c d
$ git commit -m 'ABCD'

1. a を切り離したブランチ A を作る

$ git checkout -b A base
$ git checkout ABCD a
$ git commit -m 'A'

2. b を切り離したブランチ B を作る

$ git checkout -b B base
$ git checkout ABCD b
$ git commit -m 'B'

3. c を切り離したブランチ C を作る

$ git checkout -b C base
$ git checkout ABCD c
$ git commit -m 'C'

4. d を切り離したブランチ D を作る

$ git checkout -b D base
$ git checkout ABCD d
$ git commit -m 'D'

5. base から生えてる新しい new-ABCD に A, B, C, D をマージする

$ git checkout -b new-ABCD base
$ git merge A B C D -m 'new ABCD'

6. ABCD を new-ABCD から生えてるようにリベースする

$ git rebase --onto new-ABCD base ABCD
8
11
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
8
11