Kevin Amparado

[email protected]

Git Reset isn’t scary, Reflog has your back

Ever hesitated to run git reset because you’re afraid of losing your work? You’re not alone. Many developers tiptoe around Git’s reset command, especially when using the –hard flag.

But here’s the good news: Git Reflog is your safety net.


What is Git Reset?

At its core, git reset moves the HEAD and optionally changes the index (staging area) and your working directory. There are three main types:


--soft: Keeps everything staged.

--mixed (default): Unstages everything, but keeps changes in your working directory.

--hard: Danger zone, unstages and discards working directory changes.


Scary? Maybe. But not with Reflog.


Enter Reflog: Your Git Undo Button

git reflog records every movement of HEAD, even if it’s not in the commit history anymore. That means even if you do something drastic like:

1git reset --hard HEAD~3

…and realize you just blew away your last few commits, you can get them back!


Just run:
1git reflog

Find the commit you lost, then:

1git checkout <commit_hash>
2# or even better:
3git reset --hard <commit_hash>

Real-World Example

You accidentally reset too far back:

1git reset --hard HEAD~2

Panic sets in. But wait:

1git reflog

You see:

1asdf1234 HEAD@{1}: commit: foo bar
22134asdf HEAD@{2}: commit: lorem ipsum

You can go back safely:

1git reset --hard asdf1234

Back in business.


TL;DR


#Git