LoginSignup
98

More than 1 year has passed since last update.

Git commit 取り消したい

Last updated at Posted at 2018-08-23

まとめ

  • reset : 作業を取り消す
  • revert : commit を打ち消すための commit をうつ

reset

直前の commit を index に戻す

git reset --soft HEAD~

ファイル変更と add と直前の commit を取り消す

git reset --hard HEAD~

ファイル変更と add と特定の commit を消し去る⚡️
間違えたことを歴史から消し去りたい場合に行う
HEAD は、 commit hash の値でもOK
ref. [git reset (--hard/--soft)]ワーキングツリー、インデックス、HEADを使いこなす方法 - Qiita

add したファイルを index から working directory に戻す(add を取り消す)

git reset

やべ! reset --hard 間違えてしちゃった!!

$ git reflog
019c3d2a (HEAD -> feature/fix-validation, origin/feature/fix-validation) HEAD@{0}: commit: remove
7e01141b (origin/develop, develop) HEAD@{1}: checkout: moving from develop to feature/fix-validation
7e01141b (origin/develop, develop) HEAD@{2}: pull: Fast-forward
...
$ git reset --hard HEAD@{1}

ref. `git reflog` についてまとめてみる

revert

特定の commit を打ち消す内容の commit をする
間違えた commit も歴史として残る
間違えたことを歴史として残したい場合に行う
HEAD は、 commit hash でもOK

直前を打ち消す commit をする

git revert HEAD

2つ前の commit だけを打ち消す commit をする

conflict する可能性がある

git revert HEAD~1

範囲を指定して打ち消す commit をする

git revert HEAD~7...HEAD

revert をやり直したい

conflict などして、revert に失敗
再度 revert をやり直したい時

git revert --abort

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
98