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?

More than 5 years have passed since last update.

【Git】誤ってcommitしてすぐに戻したいとき

Last updated at Posted at 2019-05-06

結論

作業メモ。結論を先に書く。

以下のコマンドをすぐに打とう。

git reset --soft HEAD^

GW中にhooksでpre-commitの検証を行っているときにアホほど打ったので、覚えてしまった…。

--soft

--softだと、「commitをしなかった状態」なので、編集していたファイルは残る。つまり、「インデックスにステージングした状態」に戻す。
間違いがなくて、安心だと思います。

--hard

これをgit reset --hard HEAD^にすると、「commit自体をなかったことにして、1つ前のcommitの状態に戻す」ことになるので、commitしたファイル も消えてしまいます。
一挙にまるまるリセットしたいときは、--hardで良いと思いますが、ちゃんと考えてからやったほうが良いでしょうね。
つまり、以下の流れを一挙にやるイメージです。

  1. git reset --soft HEAD^
  2. git reset HEAD .
  3. 対象ファイルを削除

--mixed

git reset --mixed HEAD^ は、上の箇条書きで言うところの2.に当たる部分までやるという感じです。なんだか中途半間な感じがして、あまり使わない気がします。

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?