0
0

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】途中のコミットだけを子ブランチに移す方法

Posted at

今いるブランチに追加したコミットのうち、最新のコミットではないコミットだけ別のブランチに切り出したいとうことがありました。
備忘録として、その方法を記事にしておきます。

やり方

ざっくり以下手順です。

  1. 子ブランチに移したいコミットを打ち消す
  2. 子ブランチを作成する
  3. 子ブランチに対象のコミットを反映させる

詳細は以降から説明します!

子ブランチに移したいコミットを打ち消す

コミットを打ち消すことで、現在のブランチからは対象のコミットで追加/修正した内容が消えます(打ち消したよというコミットと過去にコミットしたログは残ります)
以下がその手順の内容です。

手順は以下記事を参考にしています。

  1. git logで子ブランチに移したいコミットの sha を確認します
% git log
commit 3543b49fa0c5e25baaa6e7ed0c3ca2ce283e3c27 (HEAD -> dev/parent)
Author: hogehoge
Date:   Sun Jan 12 20:10:19 2025 +0900

    fix conflict test

commit 91feb5f9629928c7a11fdcf04aa482ce35766ae0
Author: hogehoge
Date:   Sun Jan 12 18:52:50 2025 +0900

    remove AnyView

commit ad0a3c00262fbadf472b97ac27df6216539a19e1
Merge: 0ac1ade b63e3bf
Author: hogehoge
Date:   Sun Jan 12 18:52:35 2025 +0900

    fix conflict

commit b63e3bfc419d5b200984e85da3657c21ce38ff92 (origin/BR_topic/1.0/#48_impl_graph_view_feature, BR_topic/1.0/#48_impl_graph_view_feature)
Author: hogehoge
Date:   Sun Jan 12 14:15:39 2025 +0900

    remove @CasePathable

commit 6c8a0f9ed378358163d564029048c55dcf7ed795
Author: hogehoge
Date:   Sun Jan 12 14:13:07 2025 +0900

    remove view state struct

commit 0ac1adee30d79e50ea39e44b591150c376cb4e2a // ←対象のコミット
Author: hogehoge
Date:   Sat Jan 11 11:59:06 2025 +0900

    impl calendar view(draft)

・・・(省略)
  1. 対象のコミットの sha が0ac1adee30d79e50ea39e44b591150c376cb4e2aだとわかったので、これに対してrevertを行います。

git revert 0ac1adee30d79e50ea39e44b591150c376cb4e2a

 % git revert 0ac1adee30d79e50ea39e44b591150c376cb4e2a
[BR_topic/1.0/#48_impl_graph_view_filter_area c1190cb] Revert "impl calendar view(draft)"
 Committer: hogehoge
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 8 files changed, 1 insertion(+), 292 deletions(-)
・・・(省略) 戻ったファイル名が表示されます
  1. git logrevert のコミットが生まれていることを確認
% git log
commit c1190cbbd56a658ccbe0e0608b47ba62caa8aad0 (HEAD -> dev/parent)
Author: hogehoge
Date:   Mon Jan 13 11:32:31 2025 +0900

    Revert "impl calendar view(draft)"

    This reverts commit 0ac1adee30d79e50ea39e44b591150c376cb4e2a.
commit 3543b49fa0c5e25baaa6e7ed0c3ca2ce283e3c27
Author: hogehoge
Date:   Sun Jan 12 20:10:19 2025 +0900

    fix conflict test

commit 91feb5f9629928c7a11fdcf04aa482ce35766ae0
Author: hogehoge
Date:   Sun Jan 12 18:52:50 2025 +0900

    remove AnyView

commit ad0a3c00262fbadf472b97ac27df6216539a19e1
Merge: 0ac1ade b63e3bf
Author: hogehoge
Date:   Sun Jan 12 18:52:35 2025 +0900

    fix conflict

commit b63e3bfc419d5b200984e85da3657c21ce38ff92 (origin/BR_topic/1.0/#48_impl_graph_view_feature, BR_topic/1.0/#48_impl_graph_view_feature)
Author: hogehoge
Date:   Sun Jan 12 14:15:39 2025 +0900

    remove @CasePathable

commit 6c8a0f9ed378358163d564029048c55dcf7ed795
Author: hogehoge
Date:   Sun Jan 12 14:13:07 2025 +0900

    remove view state struct

commit 0ac1adee30d79e50ea39e44b591150c376cb4e2a
Author: hogehoge
Date:   Sat Jan 11 11:59:06 2025 +0900

    impl calendar view(draft)

・・・(省略)
  1. 意図した変更になっているか確認しておく
    revert 後のコードでビルドや UT が通るかは最低限確認しておいた方が良いでしょう。
    壊れたまま子ブランチを作ったりすると、修正分をマージしたりなど後々面倒なので、問題ないことは確認しておくと後が楽かと思います。

子ブランチを作成する

現在のブランチから対象のコミットを移す先のブランチを作成します。

git checkout -b <ブランチ名>

子ブランチに対象のコミットを反映させる

コミットを反映させる方法には、チェリーピックを使用します。
コマンドは以下記事を参考にしました。

チェリーピックするにも、対象のコミット sha が必要になりますが、前回 revert するときに sha は確認しているので、そのまま使います。
具体的なコマンドは以下です。

git cherry-pick 0ac1adee30d79e50ea39e44b591150c376cb4e2a

上記コマンドは反映させたいブランチにいる状態で行います。

実際にコマンドを実行した結果が以下です。

% git cherry-pick 0ac1adee30d79e50ea39e44b591150c376cb4e2a
[dev/child 0b811a8] impl calendar view(draft)
 Date: Sat Jan 11 11:59:06 2025 +0900
 Committer: hogehoge
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 8 files changed, 292 insertions(+), 1 deletion(-)
・・・(省略)

これで完了です!
もっといいやり方ありそうなので、別の方法知っている方いらっしゃれば教えてもらえると幸いです🙇

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?