BM: Baca dalam Bahasa Melayu
World 2: Undo Magic
Detached HEAD
You will learn: how to visit an old commit and get back safely.
The problem
You want to see your diary as it was on day 1. Just look, not change. How?
Watch
git switch --detach HEAD~2 visits an old commit. HEAD now points at a commit, not a branch. This is a detached HEAD: HEAD points at a commit, not a branch. Good for looking around..
Try it
Visit the commit “Add day 1 page”.
Hint
Typegit switch --detach HEAD~2, then press Enter.Watch
Old guides use git checkout with a commit. Same result, plus a long note. Don’t panic: it’s only advice.
Predict
You are on a detached HEAD. You commit, then switch to main. What happens to that commit?
Pick one answer.
Try it
You are visiting day 1. Get back to your branch.
Hint
Typegit switch main, then press Enter.You did it!
You can now visit old commits and come back safely.
Go deeper (optional)
git switch - jumps back to where you were before. Made commits while detached? Give them a branch before you leave, or they get left behind. Making branches comes in World 3. git checkout still works in Git 3.0, next to git switch.
Bahasa Melayu
Anda nak tengok diari anda macam pada hari pertama. Tengok sahaja, bukan ubah. Macam mana?
git switch --detach HEAD~2 lawat commit (titik simpan) lama. Kini HEAD tunjuk ke commit, bukan branch (jalan cerita). Ini dipanggil detached HEAD (HEAD terlerai).
Lawat commit “Add day 1 page”.
git switch --detach HEAD~2, kemudian tekan Enter.Panduan lama guna git checkout dengan satu commit. Hasilnya sama, tambah satu nota panjang. Jangan panik: itu cuma nasihat.
Anda dalam detached HEAD. Anda buat commit, kemudian tukar ke main. Apa jadi pada commit itu?
Anda sedang melawat hari pertama. Pulang ke branch anda.
git switch main, kemudian tekan Enter.Kini anda boleh lawat commit lama dan pulang dengan selamat.
You finished this before. Practice is always welcome!
Official docs: git switch · git checkout
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?