git reflog

#git #small-print

I ran git reset --hard on the wrong branch once and spent a really unpleasant 10 minutes before somebody told me about this.

terminal
git reflog
Text

It prints everywhere HEAD has pointed in this repository (most recent first) with the command that moved it. Resets, checkouts, merges, rebases, and commits are all in there.

terminal
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
9f8e7d6 HEAD@{1}: commit: Add the delivery cutoff check
Text

The commit I thought I’d destroyed is 9f8e7d6, sitting right there. git checkout -b recover 9f8e7d6 and it’s back.

There are two things worth knowing about it. It’s local and personal so it isn’t in your coworker’s clone and it doesn’t survive a fresh clone of your own. And entries expire after 90 days by default for reachable commits and 30 for unreachable ones. So it’s a safety net for last week but not for last year.

Committing in git is pretty much permanent, and nearly every command that looks destructive is only moving a pointer. The commit is still on disk with nothing pointing at it. reflog is the list of things that used to point at it.

That’s the mental model that stuck for me. From the other direction, it’s also why a password committed once is a problem forever.

Read similar posts

View posts by tag

#accessibility #agency #ai #careers #cross-browser-compatibility #css #culture #debugging #documentation #ecommerce #forms #git #html #images #javascript #performance #php #privacy #scss #security #shopify #small-print #tooling #typography #wordpress