Help us understand the problem. What is going on with this article?

git cherry-pickで特定のコミットを完全取り消す

More than 1 year has passed since last update.

背景

ローカルでいくつかのコミットから、一つのコミットを無くしたい

$ git log -10 --oneline
84a0ea816 commit4
3259ea98b commit3
a7d8e5594 取り消したいcommit
7a1b94742 commit2
7a1b94741 commit1
7a1b94743 Merge pull request xxx(from develop)

期待する結果

$ git log -10 --oneline
84a0ea816 commit4
3259ea98b commit3
7a1b94742 commit2
7a1b94742 commit1
7a1b94743 Merge pull request xxx(from develop)

対応方法

revert

$ git revert a7d8e5594
d88adea80 Revert "xx commit"
84a0ea816 commit4
3259ea98b commit3
a7d8e5594 取り消したいcommit
7a1b94742 commit2
7a1b94742 commit1
7a1b94743 Merge pull request xxx(from develop)

ただ、これではcommitの内容を取り消しされて、コミット履歴は残ってる

cherry-pick

いい方法がなくて
developからブランチを切る
commit1,2を入れる
commit3,4を入れる

$ git co -b feature/new origin/develop
$ git cherry-pick 7a1b94743..7a1b94742
$ git cherry-pick a7d8e5594..84a0ea816

結果

$ git log -15 --oneline
84a0ea816 commit4
3259ea98b commit3
7a1b94742 commit2
7a1b94742 commit1
7a1b94743 Merge pull request xxx(from develop)

もっと簡単な方法がご存知の方はぜひコメントお願いします。(あるはず)

rebase

twitte_raruさんからコメントいただいた方法が一番良さそうです。

$ git rease -i 7a1b94743

出てきた画面で消したいcommitを消して保存する

pick 7a1b94743 Merge pull request xxx(from develop)
pick 7a1b94742 commit1
pick 7a1b94742 commit2
pick a7d8e5594 取り消したいcommit
pick 3259ea98b commit3
pick 84a0ea816 commit4

完了!一番簡単の気がします。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away