Gitでcommit
したあとにgit reset --soft
やgit reset --hard
でコミットを取り消したものの、やっぱりgit reset
をなかったことにしたい!というときの方法をメモしておきます。
git reset
を取り消す方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gitの参照ログから、もとに戻したいコミットのHEAD@{X}を確認する | |
$ git reflog | |
# git resetを取り消す | |
$ git reset --soft HEAD@{X} | |
or | |
$ git reset --hard HEAD@{X} |
コマンドのみだとわかりづらいので、実際に試してみます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# コミットログを確認する | |
$ git log --oneline | |
0328a95 (HEAD -> master) third commit | |
72710f0 second commit | |
d9c0eca first commit | |
# 直前のコミットを取り消す、変更した内容は取り消さない | |
$ git reset --soft HEAD^ | |
# 直前のコミットが取り消されている | |
$ git log --oneline | |
72710f0 (HEAD -> master) second commit | |
d9c0eca first commit | |
# git resetを取り消したい。。 | |
# Gitの参照ログを確認する | |
$ git reflog | |
72710f0 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^ | |
0328a95 HEAD@{1}: commit: third commit | |
72710f0 (HEAD -> master) HEAD@{2}: commit: second commit | |
d9c0eca HEAD@{3}: commit (initial): first commit | |
# 直前のgit resetを取り消す | |
$ git reset --soft HEAD@{1} | |
# git resetが取り消されている! | |
$ git log --oneline | |
0328a95 (HEAD -> master) third commit | |
72710f0 second commit | |
d9c0eca first commit |
git reflog
はGitのあらゆるコミット履歴を見ることができるコマンドです。git reset --soft
やgit reset --hard
でコミットを取り消した(歴史を書き換えた)としても、git reflog
でそのコミットを参照できるのです。
まとめ
git reset
を取り消す方法でした。Gitって奥が深い。。