Reflog & Recovery — Git

Pernah git reset --hard salah branch dan panik? Tenang — reflog adalah jaring pengaman lokal Git. Apa itu reflog? Setiap kali HEAD bergerak (commit, checkout, r

Pernah git reset --hard salah branch dan panik? Tenang — reflog adalah jaring pengaman lokal Git.

Apa itu reflog?

Setiap kali HEAD bergerak (commit, checkout, reset, merge, rebase), Git mencatat posisi sebelumnya di reference log lokal. Commit yang "hilang" dari git log masih ada di reflog — dan di .git/objects/ — selama 90 hari (default).

$ git reflog
abc1234 HEAD@{0}: reset: moving to HEAD~3
def5678 HEAD@{1}: commit: fitur penting
9876abc HEAD@{2}: checkout: moving from fitur to main
...
<style> .c { fill: #f59e0b; stroke: #92400e; stroke-width: 2; } .bad { fill: #ef4444; stroke: #7f1d1d; stroke-width: 2; } .lbl { font: 700 11px ui-monospace, monospace; fill: #111827; text-anchor: middle; } .act { font: 600 11px ui-sans-serif, system-ui; fill: #374151; text-anchor: middle; } .ln { stroke: #9ca3af; stroke-width: 2; fill: none; } .t { font: 700 12px ui-sans-serif, system-ui; fill: #374151; } </style> Waktu → HEAD@{4} @{3} @{2} @{1} @{0} commit checkout commit ★ merge reset --hard ← pulihkan dengan git reset --hard HEAD@{2}

Skenario penyelamatan:

1. Recover dari git reset --hard yang salah:

$ git reflog                     # cari posisi sebelum reset
$ git reset --hard HEAD@{1}      # kembali ke sana

2. Recover branch yang di-delete:

$ git reflog                     # cari hash commit terakhir branch itu
$ git branch fitur-rescue abc1234

3. Recover dari force push:

$ git reflog                     # commit lama masih ada di reflog lokal
$ git push --force-with-lease origin abc1234:main

git fsck --lost-found:

Jika reflog juga tidak cukup (commit dihapus dari reflog, atau clone baru), cari dangling objects:

$ git fsck --lost-found
dangling commit 7f2e9a...

Kapan reflog tidak bisa menyelamatkan:

Aturan emas: sebelum perintah destruktif (reset --hard, rebase, filter-branch), catat mental HEAD@{0} dulu. Reflog menyelamatkan, tapi jangan bergantung padanya untuk backup serius — gunakan git push atau tag.

Yang akan kamu pelajari