はじめに
ローカルブランチで作業中にgit commit --amend
をした際に取り消そうと思ったのに、キーボードの打ち間違えでエディタが保存となってしまって、困ったことが何回かあり、その解決方法をまとめました。
実行手順
-
git reflog
で、コミットの履歴が一覧表示されます。
※間違えて、initial commitにammendしてしまった場合
```sh:ターミナル
git reflog
02e93b2 HEAD@{0}: commit (amend): Initial commit
cd9c949 (origin/main, main) HEAD@{1}: checkout: moving from main to branch
cd9c949 (origin/main, main) HEAD@{2}: commit (amend): Initial commit
```
-
git reset --soft HEAD@{番号}
で、そのコミット履歴の過去の状態に戻ります。
```sh:ターミナル
git reset --soft HEAD@{2}
```