$shibayu36->blog;

クラスター株式会社のソフトウェアエンジニアです。エンジニアリングや読書などについて書いています。

git revertで複数コミットを巻き戻す方法

 今日git revertを使っていて、少しわかりづらかったのでメモ。実はこのこと昔に書いていたんだよね。http://d.hatena.ne.jp/shiba_yu36/20100221/1266680669

 さて、次のコマンドを打つと、単一のコミットを巻き戻す内容のコミットがなされます。このコマンドを打った瞬間にコミットされてしまうので、複数のコミットを巻き戻すコミットを作れません。

git revert (コミット名)

 しかし、git revertには-nオプションというものがあり、これを使えば複数コミットを巻き戻すコミットを作れます。-nオプションのヘルプを見ると

  • n, --no-commit

Usually the command automatically creates a commit with a commit log message stating which commit was reverted. This flag applies the change necessary to revert the named commit to your working tree and the index, but does not make the commit. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

と書いてあります。簡単に言えば、巻き戻す変更をいったんステージングにのせて、コミットはしないでおくオプションです。つまりこれを次のように使えば、複数コミットを一つのコミットで巻き戻せるわけです。

git revert -n (コミット名)
git revert -n (コミット名)
git revert -n (コミット名)
git commit

以上です。