今日已更新 279 条资讯 | 累计 38972 条内容
关于我们

I Ran git reset --hard in the Wrong Window

Rohit Bhadani 2026年09月03日 11:38 1 次阅读 来源:Dev.to

git reset --hard HEAD~3 — run in the wrong repository window, at 6:40pm, immediately followed by the specific kind of silence that happens when you realize what you just did before your brain finishes processing it. Three commits of uncommitted-adjacent work, gone from the working tree in under a second. The first, most important fact: it's very likely still there git reset --hard moves the branch pointer and resets the working tree, but Git doesn't actually delete commit objects just because nothing points at them anymore — they sit in the object database, unreferenced, until garbage collection eventually cleans them up, which for most repos happens rarely enough that "eventually" can mean weeks. git reflog a1b2c3d HEAD@{0}: reset: moving to HEAD~3 e4f5g6h HEAD@{1}: commit: add retry logic to payment webhook 7h8i9j0 HEAD@{2}: commit: fix currency rounding k1l2m3n HEAD@{3}: commit: initial webhook handler The reflog is a local log of everywhere HEAD has pointed recently, and it survives a reset because a reset is just another entry in it, not an erasure of the ones before it. git reset --hard e4f5g6h Working tree restored to exactly the state before the reset, all three commits back, in the time it takes to read this sentence. When the reflog isn't enough If the commits were never made at all — you ran reset --hard on genuinely uncommitted changes — the reflog can't help, because it only tracks where HEAD and branches have pointed, not file contents that were never committed. That's a real loss, and the only real defense against it is committing early and often, including throwaway "wip" commits you intend to squash later, specifically because an uncommitted change has no recovery path at all. If the commits were committed and the reflog entry has expired — Git's default is to keep unreachable reflog entries for 90 days, reachable ones for longer — git fsck --unreachable can sometimes still find dangling commit objects directly: git fsck --unreachable --no-reflog |

本文内容来源于互联网,版权归原作者所有
查看原文