読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第5節 消えたものを探す

5.3 消したブランチを戻す

まだ取り込んでいないブランチを消そうとすると、Gitは止めます。

git branch -d price-update
error: the branch 'price-update' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D price-update'

-D で強制的に消すと、そのブランチだけにあったコミットは、どの名前からもたどれなくなります。

git branch -D price-update
Deleted branch price-update (was 99718ef).

消したときの表示に、ブランチの先端の番号(99718ef)が出ています。この番号でブランチを作り直せば、元に戻ります。

git branch price-update 99718ef
git log --oneline -1 price-update
99718ef 価格表の見直し

表示を見逃したときは、git reflog でそのブランチで最後にしたコミットを探します。

ab5a35a HEAD@{0}: checkout: moving from price-update to main
99718ef HEAD@{1}: commit: 価格表の見直し

moving from price-update to main の1つ下の行が、price-update を離れる直前のコミットです。

解答

9f8e7d6(HEAD@{1})。reset: moving to HEAD~2 の1つ下の行が、resetの直前の状態です。git reset --hard 9f8e7d6 で、2件のコミットが戻ります。

28 / 36