2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-01-30

背景

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

$ 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

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

2
4
2

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?