BM: Baca dalam Bahasa Melayu
World 11: Rescue Center
I lost commits after reset
You will learn: how to bring commits back after a hard reset.
The problem
Oops. One git reset --hard HEAD~2, and two days of diary vanished. Breathe. This happens to everyone. Here’s the safe way out.
Try it
Day 2 and day 3 were commits. Can Git bring them back?
Pick one answer.
Watch
git reflog lists every move HEAD made. HEAD@{1} is where you were just before the reset.
Try it
Jump back to just before the reset.
Hint
Typegit reset --hard HEAD@{1}, then press Enter.Check
Put the rescue steps in order, first at the top.
First step at the top. Put these in order. Use the arrows, or drag.
- git reflog: find the line before the reset
- git log --oneline: check the commits are back
- git reset --hard HEAD@{1}
- git status: check nothing is uncommitted
Hint
--hard wipes uncommitted changes. So look before you jump.You did it!
You can now bring commits back after a hard reset.
Go deeper (optional)
Extra careful? git branch rescue HEAD@{1} only puts a label on the lost commit. Nothing moves, nothing is wiped. Right after a reset, ORIG_HEAD also points where you were. Changes you never committed can’t come back. Commit often!
Bahasa Melayu
Alamak. Satu git reset --hard HEAD~2, dan dua hari diari lenyap. Tarik nafas. Ini berlaku pada semua orang. Ini cara selamat untuk keluar.
Hari 2 dan hari 3 ialah commit (titik simpan). Boleh Git bawa balik?
git reflog senaraikan setiap langkah HEAD. HEAD@{1} ialah tempat anda sejurus sebelum reset.
Lompat balik ke sejurus sebelum reset.
git reset --hard HEAD@{1}, kemudian tekan Enter.Susun langkah menyelamat, yang pertama di atas.
--hard padam perubahan yang belum di-commit. Jadi tengok dulu sebelum lompat.Kini anda boleh bawa balik commit selepas reset –hard.
You finished this before. Practice is always welcome!
Official docs: git reflog · git reset · Revision names (HEAD@{1})
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?